need help with finding errors

This is a discussion on need help with finding errors within the C Programming forums, part of the General Programming Boards category; Code: //question 1 int main() { int choice = -1, num1,num2,trials = 0; //give the user a max of five ...

  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    40

    need help with finding errors

    Code:
    //question 1
    int main()
    {
    int choice = -1, num1,num2,trials = 0;
    
    
    //give the user a max of five trials
    while((choice = -1) && trials <5)
    {
        printf("Enter a number : ");
        scanf("%d",&num1);
        printf("Enter another number: ");
        scanf("%d",&num2);
        printf("Their sum is %d\n",(num1+num2));
        printf("Enter -1 to repeat, 1 to exit");
        scanf("%d",&choice);
        trials++;
    }
        return 0;
    }
    
    
        
        
        
    //question 2
    int a = -75;
    
    
    if(a != 0)
    {
        printf("The number is different from 0\n");
        printf("Please enter another number \n");
    }
    else if (a<0)
        printf("The number is negative \n");
    else 
        printf("The number is positive \n");
    There are 2 questions, as I posted in the above.
    The question says there is one error in each questions.
    I tried to find errors but, I couldn't find any....
    I even tried to run the program and it worked fine.
    Could anyone help me with finding errors?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,739
    For the first one, try exiting the program early. For the second one, try to obtain the output "The number is negative \n".
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Feb 2013
    Posts
    40
    Thank you very much for your help!!!
    I think I found errors...
    For the first one, there is no statement to exit the program and for the second one, there is "else if" instead of "if".

  4. #4
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,650
    Code:
    while((choice = -1) && trials <5)
    Note that = is an assignment, not a comparison.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. GPA and finding a job
    By camel-man in forum General Discussions
    Replies: 3
    Last Post: 10-17-2011, 06:18 AM
  2. Help me finding the errors PLEASE !!!
    By Salmi in forum C++ Programming
    Replies: 6
    Last Post: 01-28-2010, 01:49 AM
  3. Finding OS
    By Chandana in forum C Programming
    Replies: 13
    Last Post: 08-08-2009, 03:01 PM
  4. Need help finding bug
    By Guti14 in forum C++ Programming
    Replies: 2
    Last Post: 10-05-2003, 02:21 AM
  5. errors.. errrors.. more errors
    By Klinerr1 in forum C++ Programming
    Replies: 17
    Last Post: 07-23-2002, 08:43 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21