User Name Password Register
DaniWeb IT Discussion Community
All
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
Reply
Join Date: Jul 2006
Posts: 32
Reputation: whitemoss is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 0
whitemoss whitemoss is offline Offline
Light Poster

if statement didnt work as expected

  #1  
Dec 10th, 2006
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:

// 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.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: May 2006
Posts: 2,698
Reputation: WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold 
Rep Power: 14
Solved Threads: 219
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Maven

Re: if statement didnt work as expected

  #2  
Dec 11th, 2006
Originally Posted by whitemoss View Post
need ur help regarding the above matter.
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.

Originally Posted by whitemoss View Post
I've write a code for if statement but it didnt work as expected when I run the program.
I assume you mean the IF statments like:
if (sprintf(query2, 
            "select * from Register where MasterAccNo = 'NONE'") 
                    == 0);
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.
Age is unimportant -- except in cheese
Reply With Quote  
Join Date: Jul 2006
Posts: 32
Reputation: whitemoss is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 0
whitemoss whitemoss is offline Offline
Light Poster

Re: if statement didnt work as expected

  #3  
Dec 11th, 2006
Hi WaltP,

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
Reply With Quote  
Join Date: Oct 2006
Location: the Netherlands
Posts: 1,562
Reputation: niek_e is a jewel in the rough niek_e is a jewel in the rough niek_e is a jewel in the rough niek_e is a jewel in the rough 
Rep Power: 8
Solved Threads: 158
niek_e's Avatar
niek_e niek_e is offline Offline
Posting Virtuoso

Re: if statement didnt work as expected

  #4  
Dec 11th, 2006
What he means is that this piece of code:
 
if (sprintf(query3, "select * from Register where substring(MasterNumber,2,1) = '1' and MasterAccNo <> 'NONE'") == 0)
Will always be false. Sprintf will return the value of chars loaded into query3.
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...
Reply With Quote  
Join Date: May 2006
Posts: 2,698
Reputation: WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold 
Rep Power: 14
Solved Threads: 219
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Maven

Re: if statement didnt work as expected

  #5  
Dec 12th, 2006
Originally Posted by whitemoss View Post
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
Reply With Quote  
Join Date: Jul 2006
Posts: 32
Reputation: whitemoss is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 0
whitemoss whitemoss is offline Offline
Light Poster

Re: if statement didnt work as expected

  #6  
Dec 12th, 2006
Hi Guys...

Thanks for ur help..my problem already solved..
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb C Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the C Forum

All times are GMT -4. The time now is 9:28 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC