Problem comparing variables

This is a discussion on Problem comparing variables within the C++ Programming forums, part of the General Programming Boards category; Code: #include <iostream> using namespace std; int main() { int num1, num2; cout<<"Please enter a number for number one:"; cin>> ...

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    6

    Problem comparing variables

    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
        int num1, num2;
        cout<<"Please enter a number for number one:";
        cin>> num1;
        cout<<"You've entered: "<< num1 <<" for number one.\n";
        cout<<"Please enter a number for number two:";
        cin>> num2;
        cout<<"You've entered: "<< num2 <<" for number two.\n";
        cout<<"Now calculating results....Please wait...\n";
        if (num1 < num2);{
                 cout<< num2 <<"is greater than" << num1 <<"\n";
                 }
        else (num1 > num2);{
                 cout<< num2 <<"is less than" << num1 <<"\n";
                 }
        main();
    }
    When compiling, I get this error,

    Code:
     In function `int main()':
    17 expected primary-expression before "else"
    17 expected `;' before "else"
    I've always had problems with If and Else statements. Anyone wanna help meh out?
    Last edited by Deathscreton; 08-15-2009 at 03:00 PM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,744
    You have extra semi-colons on these lines:
    Code:
    [if (num1 < num2);{
    Code:
    else (num1 > num2);{
    Also, recursively calling the global main function is not allowed in C++.
    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
    Aug 2009
    Posts
    6
    Ah, I see. Very well. Thank you kindly. I'll repost here if I come over anymore problems.

  4. #4
    Registered User
    Join Date
    Aug 2009
    Posts
    6
    I came across a new problem, but fixed it fairly quickly. Else isn't supposed to have a statement. Anyways, I killed the code, and rebuilt it from ground up using what I just learned. But instead of meeting success, I met multiple errors. This is the code.

    Code:
    #include <iostream>
    using namespace std;
    
    
    int main()
        {
              char letter;
              char letter2;
              int num1, num2;
              
                  main();
                  
    }
    
    int number1()
        {
                 cout<<"Please enter a number for number 1.\n";
                 cin>> num1;
                 cout<<"You've entered" << num1 << "for number 1. Is that correct?\n";
              cout<<"Press y for yes, n for no.\n";
              if (letter == y)
              { 
                          number2();
                         }
              else
              {
                   number1();
                  }
                 }
    int number2()
        {
                 cout<<"Please enter a number for number 2.\n";
                 cin<< num2;
                 cout<<"You've entered" << num2 <<"for number2, is this correct?\n";
                 cout<<"Press y for yes, n for no.\n";
                 if (letter == y)
                 {
                            main();
                            }
                 else{
                      number2();
                      }
    Too many errors to list. Do you think you could tell what's wrong by looking at the code? Or you could obtain the errors by compiling the code yourself.

  5. #5
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Code:
    #include <iostream>
    using namespace std;
    
    
    int main() {
        char letter;
        char letter2;
        int num1, num2;
    
        main();
    
    }
    
    int number1() {
        cout<<"Please enter a number for number 1.\n";
        cin>> num1;
        cout<<"You've entered" << num1 << "for number 1. Is that correct?\n";
        cout<<"Press y for yes, n for no.\n";
        if (letter == y) {
            number2();
        } else {
            number1();
        }
    }
    int number2() {
        cout<<"Please enter a number for number 2.\n";
        cin<< num2;
        cout<<"You've entered" << num2 <<"for number2, is this correct?\n";
        cout<<"Press y for yes, n for no.\n";
        if (letter == y) {
            main();
        } else {
            number2();
        }
    Now we can start looking at the code.

    What you have is main() that declares a few variable, then calls itself again. main() is a special function: you don't call it, it is called for you when your program starts.

    But the errors start from having undefined variables in the rest of the functions. Variables declared within a function are local to that function. These names are not visible in other functions.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You can't call main(). Ever ever ever ever ever ever ever.

    Your main doesn't do anything.

    Your number1 function refers to variables that don't exist, like num1 and y, as well as trying to call a function number2 that has not been declared.

    Ditto number2 trying to use variables num2 and y.

  7. #7
    Registered User CrissyCrisCris's Avatar
    Join Date
    Aug 2009
    Posts
    13
    Is it just me or is there no return values in the code at all? main(), number1(), and number2() are all int type and yet they return nothing.

    Also...

    Code:
        char letter;
        char letter2;
        int num1, num2;
    These are declared in main(), but used in number1() and number2(). You have to declare them as global variables to do that.

    And if you want to use number1() and number2() under main() you have to use prototypes.

    Code:
    int number1();
    int number2();
    You are comparing an empty cariable letter to y.

    Code:
    if (letter == y)
    You have to get an input for letter first. Like this:

    Code:
    char letter[2];
    cin.getline(letter, 1);
    //Then compare them with strcmp()
    if (strcmp(letter, "y") == 0)
         //...
    If you try to run that you'll get something about main() having an int type with no return value.

    If you get passed that, you will jsut be running main() in a contuinous loop. You have to call number1() and number2() from main.

  8. #8
    Registered User
    Join Date
    Aug 2009
    Posts
    6
    Obviously, I screwed that up. I was editing the code before I posted it. So that main calling main was a complete and idiotic accident on my part. And making variables global, they would have to be called out of all functions before Main, correct? And i've called main before, it just repeats itself, the program never ending. :/

  9. #9
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,189
    Quote Originally Posted by Deathscreton View Post
    And i've called main before, it just repeats itself, the program never ending. :/
    With ever ever ever increasing stack usage until your application crashes. In general recursive operations should have at least some possible way of eventually returning.
    Until you can build a working general purpose reprogrammable computer out of basic components from radio shack, you are not fit to call yourself a programmer in my presence. This is cwhizard, signing off.

  10. #10
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,614
    Quote Originally Posted by CrissyCrisCris View Post
    You have to get an input for letter first. Like this:

    Code:
    char letter[2];
    cin.getline(letter, 1);
    //Then compare them with strcmp()
    if (strcmp(letter, "y") == 0)
         //...
    If you try to run that you'll get something about main() having an int type with no return value.

    If you get passed that, you will jsut be running main() in a contuinous loop. You have to call number1() and number2() from main.
    Very bad example. This screams C approach. A better C++ way is:
    Code:
    std::string letter;
    std::getline(std::cin, letter);
    if (letter == "y")
         //...
    Or simply
    Code:
    char letter;
    std::cin >> letter;
    if (letter == 'y')
         //...
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Registered User
    Join Date
    Aug 2009
    Posts
    6
    I'm not sure about using the std command. I'm still trying to work out this program and remove all bugs before moving on. I want to make sure I have this entire thing down.

    And if you don't advise using main(); to repeat the program, ((Which i will eventually stop using once my program becomes too large)) what would you suggest I use?

  12. #12
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Use any of the common mechanisms such as while, do - while or for. Even goto would be better than calling main recursively [recursively here means calling it from a call that somehow came from main], but goto is frowned upon in programming in general, so you'd better be prepared to explain why you think that is a good solution if you choose to do so - and the fact that "you think that's easiest" is not an acceptable solution if you want to be considered a serious programmer - that's like a carpenter explaining why he is using a hammer to put a screw into two bits of wood by saying "it's easier".

    As has been mentioned, main in C++ is special. It may well do things at the beginning that should not be done again. One of those things is constructor calls for global objects - this needs to be done before "your code" inside main is started, but the compiler has little control over when exactly before main() it can safely and accurately call the constructors. If you do not understand this explanation, please just accept that "main is special, and can not be (safely) called from other parts of your code". [1]


    [1] The exception to this rule is if you are actually working with the runtime-library code that actually DOES call main during program startup. But as you are a beginner, this is not relevant either. I'm just covering this so that some other pedant doesn't gets upset by the lack of detailed explanation. Please ignore this too!

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  13. #13
    Registered User
    Join Date
    Aug 2009
    Posts
    6
    Understandable. Once I reach that stage, I'll begin to implement it into my source code. But until then, I'll have to deal with the bugs already in my program. For instance, the program tends to drop the first integer variable...All the time. It turns from what ever number the user inputs, to zero. Here's the code. There are no errors.

    Code:
    #include <iostream>
    using namespace std;
    
    int number1(char,int); //My first function
    int number2(char,int);//My second function
    int result(int,int);//My Third function
    int num1,num2,;//Integer variables I will be using
    char letter;//The variable that will contain the answer choices.
    
    int main()
    {
        number1(letter,num1);//This is my first function call
        cin.get();
        return 0;
    }
    
    int number1(char letter, int num1)//This is the function being called and working in action.
    {
        cout<<"Please enter a number for num1.\n";
        cin>> num1;
        cin.ignore();
        cout<<"You've entered "<< num1 <<" for num1. Is this correct?\n";
        cout<<"Type'y' then enter for yes; 'n' then enter for no.\n";
        cin>> letter;
        cin.ignore();
        if (letter == 'y')
        {
                      number2(letter,num2);//This is the continuation of the function calls.
                      }
                      else
                      {
                          number1(letter,num1);//This is redirecting itself back to the function
                          }                    //so the number variable can be edited by the user.
                          }
    int number2(char letter, int num2)//This is my function that has been called by the previous 
    {                                 //function. 
        cout<<"Please enter a number for num2.\n";
        cin>> num2;
        cin.ignore();
        cout<<"You've entered "<< num2 <<" for num2. Is this correct?\n";
        cout<<"Type 'y' then enter for yes; 'n' then enter for no.\n";
        cin>> letter;
        cin.ignore();
        if (letter == 'y')
        {
                      cout<<"Calculating results, please wait....\n";
                      result(num1,num2);//The final and last function.
                      }
                      else
                      {
                          number2(letter,num2);//Function calling itself to be edited by user.
                          }
                          }
    int result(int num1, int num2)//Last function comparing to two integers.
    {
        if (num1 < num2)//I don't know where in the program exactly, but the integer for num1
        {               //is somehow lost before here and turned into a zero.
                 cout<< num1 <<" is less than "<< num2 << ".\n";
                 }
                 
                 else if (num1 == num2){
                      cout<< num1 <<" is equal to "<< num2<<".\n";
                 }
                 else
                 { cout<< num1 <<" is greater than "<< num2<<".\n";
                 }
    }
    Last edited by Deathscreton; 08-16-2009 at 08:10 PM. Reason: Adding a comment to my last function

  14. #14
    Registered User
    Join Date
    Jun 2005
    Posts
    6,307
    Quote Originally Posted by Deathscreton View Post
    I'm not sure about using the std command.
    std is not a command, it is the name of a namespace. A namespace is, roughly, just a collection of names (or, in geometric terms, a space of names).

    In the line
    Code:
    std::getline(std::cin, letter);
    std::cin is an object named cin placed within a namespace named std. Another way of saying that is that "std::cin" is a complete name of an object named cin within the namespace std.

    Similarly, std::getline() is a function named getline which is placed within a namespace named std.
    Right 98% of the time, and don't care about the other 3%.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 10:22 AM
  2. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  3. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  4. Problem with global variables
    By DominicTrix in forum C++ Programming
    Replies: 6
    Last Post: 09-08-2004, 01:26 PM
  5. Replies: 4
    Last Post: 10-17-2002, 10:09 PM

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