Thread: Problem comparing variables

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    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).

  2. #2
    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.

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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.

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, 11: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