•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the C section within the Software Development category of DaniWeb, a massive community of 391,554 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,603 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C advertiser:
Views: 1100 | Replies: 5 | Solved
![]() |
•
•
Join Date: Jul 2006
Posts: 32
Reputation:
Rep Power: 3
Solved Threads: 0
Hi guys,
need ur help regarding the above matter. I've write a code for if statement but it didnt work as expected when I run the program. Below is my code:
When I run this program, it executes all three conditions. Supposedly, only 1 condition/query matched and will be executed and then write into a file but then, in this case, all three queries executed caused 3 same data written into a file.
The result written in the file when I run this program is:
supposedly this account will be 2P_SMS_TM
Any help?
Many thanks
need ur help regarding the above matter. I've write a code for if statement but it didnt work as expected when I run the program. Below is my code:
// Get ChannelID
strcpy(src, row[11]);
if (strcmp (src, "1") == 0)
{
if (sprintf(query2, "select * from Register where MasterAccNo = 'NONE'") == 0);
{
strcpy (src, "2P_SMS_RegisteredXPax");
strcpy(src2, src);
printf ("%s\n",src2);
sprintf(string2,"%s|%s|%s|%s|%s,%s|%s,%s|%s|%s|%s|||||%s|",
row[0], row[1], row[2], row[3], row[4], row[5], row[6], row[7], row[8], var2, newDate, src2);
sprintf(mobileno,"%s", row[0]);
printf ("%s\n",string2);
writeLogFile(string2, mobileno);
//i++;
}
if (sprintf(query3, "select * from Register where substring(MasterNumber,2,1) = '1' and MasterAccNo <> 'NONE'") == 0);
{
strcpy (src, "2P_SMS_PostpaidAccount");
strcpy(src3, src);
printf ("%s\n",src3);
sprintf(string3,"%s|%s|%s|%s|%s,%s|%s,%s|%s|%s|%s|||||%s|",
row[0], row[1], row[2], row[3], row[4], row[5], row[6], row[7], row[8], var2, newDate, src3);
sprintf(mobileno,"%s", row[0]);
printf ("%s\n",string3);
writeLogFile(string3, mobileno);
//i++;
}
//else
if (sprintf(query4, "select * from Register where substring(MasterNumber,2,1) <> '1' and MasterAccNo <> 'NONE'") == 0);
{
strcpy (src, "2P_SMS_TM");
strcpy(src4, src);
printf ("%s\n",src4);
sprintf(string4,"%s|%s|%s|%s|%s,%s|%s,%s|%s|%s|%s|||||%s|",
row[0], row[1], row[2], row[3], row[4], row[5], row[6], row[7], row[8], var2, newDate, src4);
sprintf(mobileno,"%s", row[0]);
printf ("%s\n",string4);
writeLogFile(string4, mobileno);
//i++;
}
}
i++;When I run this program, it executes all three conditions. Supposedly, only 1 condition/query matched and will be executed and then write into a file but then, in this case, all three queries executed caused 3 same data written into a file.
The result written in the file when I run this program is:
0135767903|WONG HWA HUONG|49022811355468||4A, LRG MERDEKA 2,96000 SIBU|,|96000|MALAYSIA|30-11-2006|||||2P_SMS_RegisteredXPax| 0135767903|WONG HWA HUONG|49022811355468||4A, LRG MERDEKA 2,96000 SIBU|,|96000|MALAYSIA|30-11-2006|||||2P_SMS_PostpaidAccount| 0135767903|WONG HWA HUONG|49022811355468||4A, LRG MERDEKA 2,96000 SIBU|,|96000|MALAYSIA|30-11-2006|||||2P_SMS_TM|
supposedly this account will be 2P_SMS_TM
Any help?
Many thanks
Last edited by whitemoss : Dec 10th, 2006 at 9:52 pm.
Please state the matter in the post. Don't use the title as part of your question. It's only meant to give us an idea about what we're about to read.
I assume you mean the IF statments like:
What the sprintf() statement does is load the first parameter (query2) with the rest of the parameters and returns the number of characters loaded. Therefore, each and every IF will fail.
But notice the character at the end of the IF (in red), that will end the IF. Therefore the code block is executed because it's not part of the IF at all.
•
•
•
•
I've write a code for if statement but it didnt work as expected when I run the program.
if (sprintf(query2,
"select * from Register where MasterAccNo = 'NONE'")
== 0);But notice the character at the end of the IF (in red), that will end the IF. Therefore the code block is executed because it's not part of the IF at all.
Age is unimportant -- except in cheese
•
•
Join Date: Jul 2006
Posts: 32
Reputation:
Rep Power: 3
Solved Threads: 0
Hi WaltP,
Thanks for ur reply. I get what do u mean for this statement:
but for this statement:
how should i modify my IF statement in order for the query to execute properly based on the conditions provided?..
Many thanks
Thanks for ur reply. I get what do u mean for this statement:
•
•
•
•
But notice the character at the end of the IF (in red), that will end the IF. Therefore the code block is executed because it's not part of the IF at all.
but for this statement:
•
•
•
•
What the sprintf() statement does is load the first parameter (query2) with the rest of the parameters and returns the number of characters loaded. Therefore, each and every IF will fail.
how should i modify my IF statement in order for the query to execute properly based on the conditions provided?..
Many thanks
•
•
Join Date: Oct 2006
Location: the Netherlands
Posts: 1,562
Reputation:
Rep Power: 8
Solved Threads: 158
What he means is that this piece of code:
Will always be false. Sprintf will return the value of chars loaded into query3.
So in this case it would return 89 because
Regards Niek
if (sprintf(query3, "select * from Register where substring(MasterNumber,2,1) = '1' and MasterAccNo <> 'NONE'") == 0)
So in this case it would return 89 because
select * from Register where substring(MasterNumber,2,1) = '1' and MasterAccNo <> 'NONE' is 89 chars (including spaces) long.Regards Niek
do NOT pm me for help, it makes me angry. You wouldn't like me when I'm angry...
•
•
•
•
how should i modify my IF statement in order for the query to execute properly based on the conditions provided?..
You are using the wrong process.
1) Load the query string into the variable (the sprintf() part)
2) Execute the query using some database function. This you didn't do.
3) Now check the return from the database call with the IF
Age is unimportant -- except in cheese
![]() |
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
•
•
•
•
•
•
•
•
DaniWeb C Marketplace
- if then statement (C++)
- Need Help on SQL Query for select statement (MS SQL)
- My phpbb forum doesnt work ? (PHP)
- i cant get my realtek card to work in rh8 (*nix Hardware Configuration)
Other Threads in the C Forum
- Previous Thread: I/O device simulation
- Next Thread: Program works... now I need to create a function, HELP!



Linear Mode