CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Oct 2009
    Posts
    8

    Logic Error (Simple IF Statement)

    I have the following block of code...


    Code:
           int counter2 = 0;
    
           while(counter2 <  1) {
    
            printf("What time of the day were you travelling at? Was it at the peak
                    scanf("%d", &timeofday);
    
            if(timeofday == 1 || timeofday ==2) {
                    counter2 = counter2 + 1;
                                                                     }
            else
                    printf("ERROR; incorrect input.");
    
                                             }
    I wanted to keep prompting the user for this question until the integer "1" is entered. I don't think the IF statement is ever true regardless if I were to enter a 1, or not. Simply because I am comparing the input from the user with an integer, which i believe is the problem. But.. how would i be able to fix this?

  2. #2
    Join Date
    Mar 2004
    Location
    Prague, Czech Republic, EU
    Posts
    1,701

    Re: Logic Error (Simple IF Statement)

    1. parse the input to get an integer
    2. compare string to string, something like
    Code:
     if(timeofday == "1" || timeofday == "2") {
    • Make it run.
    • Make it right.
    • Make it fast.

    Don't hesitate to rate my post.

  3. #3
    Join Date
    Oct 2009
    Posts
    8

    Re: Logic Error (Simple IF Statement)

    Quote Originally Posted by boudino View Post
    1. parse the input to get an integer
    2. compare string to string, something like
    Code:
     if(timeofday == "1" || timeofday == "2") {
    When I tried to use the quotations and compare string to string, the compiler said that i was trying to compare between a pointer and an integer.

  4. #4
    Join Date
    Jan 2002
    Location
    Scaro, UK
    Posts
    5,940

    Re: Logic Error (Simple IF Statement)

    2. compare string to string, something like
    The code sample appears to be C++ - try the c++ forums not the C# forums.

    Darwen.
    www.pinvoker.com - PInvoker - the .NET PInvoke Interface Exporter for C++ Dlls.

  5. #5
    Join Date
    Oct 2009
    Posts
    8

    Re: Logic Error (Simple IF Statement)

    Quote Originally Posted by darwen View Post
    The code sample appears to be C++ - try the c++ forums not the C# forums.

    Darwen.
    This is what I have tried to do in my attempt to parse the user input, still generate error.

    Code:
             int timeParse = 0;
    do{
    
           while(counter2 < 1) {
            printf("What time of the day were you travelling at? Was it at the peak 
                    scanf("%d", &timeofday);
                    int timeParse = int.Parse(timeofday);
    
    
                    printf("made it here");
                    counter2 = counter2 + 1;
            if(timeParse == 1 || timeParse  == 2){
                    counter2 = counter2 + 1;
                                                }
            else{
                    printf("ERROR; Please enter a valid input.");
                }
           }
    ERROR: assignment1.c:16: error: expected expression before 'int'

  6. #6
    Join Date
    Jan 2002
    Location
    Scaro, UK
    Posts
    5,940

    Re: Logic Error (Simple IF Statement)

    scanf and printf are both C/C++ methods. They don't exist in C#.

    Are you trying to write a C# application or a C/C++ application ?

    You are aware that they are completely different languages ?

    Darwen.
    www.pinvoker.com - PInvoker - the .NET PInvoke Interface Exporter for C++ Dlls.

  7. #7
    Join Date
    Mar 2004
    Location
    Prague, Czech Republic, EU
    Posts
    1,701

    Re: Logic Error (Simple IF Statement)

    I think the intention is clear, rewrite it to the language you are using. I think that it could be something like
    Code:
    if(timeofday* == "1" || timeofday* == "2")
    • Make it run.
    • Make it right.
    • Make it fast.

    Don't hesitate to rate my post.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured