Help with "cin" in C++

This is a discussion on Help with "cin" in C++ within the C++ Programming forums, part of the General Programming Boards category; Originally Posted by elad Posting your new code would be the best thing to do at this point. My best ...

  1. #16
    Registered User
    Join Date
    Jul 2005
    Posts
    11
    Quote Originally Posted by elad
    Posting your new code would be the best thing to do at this point. My best guess about this:

    putting int answerget and cin >> answerget in startmenu(), I get a lot of different errors.

    is that you may be trying to use the value of answerget back in main() but you aren't using the correct syntax to transfer the value of answerget from startmenu() back to main().
    How do I transfer that value?

  2. #17
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    When you declare a variable inside a function, once the function ends, the variable is destroyed and no longer exists, so you can't refer to the variable outside the function, i.e. in main().

    You either have to declare the variable in main() and pass it to the function, or return a value from the function which you store in another variable.

    Here is a very simple function:
    Code:
    #include <iostream>
    using namespace std;
    
    void showNumber()
    {
       cout<<100<<endl;
    }
    
    int main ()
    {
       showNumber();
    
       return 0;
    }
    Functions must specify a 'return type', and the return type precedes the function name. In the function above, there is no return value, and that is designated by 'void'. The function is 'called' in main() by this line:

    showNumber();

    That says, "please go find a function named showNumber, and execute the lines of code contained therein."

    Here is another example:
    Code:
    #include <iostream>
    using namespace std;
    
    void showNumber(int a)
    {
       cout<<a<<endl;
    }
    
    int main ()
    {
       showNumber(100);
       showNumber(25);
    
       return 0;
    }
    Now, instead of always displaying the same number, you can send the function a specific number, and the function will display whatever number you send it. Once again, the function does not return a value, so its return type is 'void'. You should note that the type of the number you send to the function must match the function 'parameter'. The function parameter above is "int a", which means the function is expecting an int type to be 'sent' to it. You send a value to a function like this:

    showNumber(100);

    To send a value to a function, you just put the value between the parentheses after the function name. The function parameter "int a" creates an integer variable named "a" which stores the value you send to the function--that's why the type of the value you send has to match the function parameter. Then, inside the function you can use the variable "a" by referring to it by name:

    cout<<a<<endl;

    Here is another example:
    Code:
    #include <iostream>
    using namespace std;
    
    int addTen(int a)
    {
       int answer = a + 10;
       return answer;
    }
    
    int main ()
    {
       int result1 = addTen(2);
       int result2 = addTen(5);
    
       cout<<"Here are the results: "<<result1<<" "<<result2<<endl;
       
       return 0;
    }
    This time the function returns a value. The return type preceding the function name must match the type of the value in the 'return statement'. The return statement replaces the function call in main(), e.g. addTen(2), with the return value. So, result1 is assigned 12, and result2 is assigned 15.

    To summarize:
    1) A function must list a return type before its name.
    2) The return type must match the type of the value in the return statement--if there is no return statement, then the type is 'void'.
    3) When you call a function, the value you send it, must match the function parameter.
    4) The function call in main() is replaced by the function return value.
    5) Sometimes if a function doesn't have any parameters, like in the first example, "void" will be used for the parameter. For instance you might see the function in the first example written like this:
    Code:
    void showNumber(void)
    {
       cout<<100<<endl;
    }
    Last edited by 7stud; 07-27-2005 at 11:40 AM.

  3. #18
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    When you get a handle on 7studs reply then you can learn the other way to accomplish transfer of information between functions---passing by reference (using either a pointer or a reference) as opposed to passing by value and using a return value to accomplish the transfer (which is what 7stud told you about, and which you should understand very well before moving on, IMO).
    You're only born perfect.

  4. #19
    Registered User
    Join Date
    Jul 2005
    Location
    US
    Posts
    6

    7stud, U should try the two sample codes by ILoveVectors

    7stud, try testing the two sample codes posted by ILoveVectors and type in "abcd" in the console if you want an example of the "baffling behaviour".

    ILoveVectors, thanks for the advice on the usage of cin.ignore(). I used your two example codes and tested your explanation and you are right about "if you try to enter more character then a, b, or c, can hold
    then ignore removes them off the buffer so not to affect
    the other cin >> statments.
    "

    From my other post in a similar topic about cin.ignore(), Shakti showed me how to use cin.ignore() and cin.get() to pause the console at the end of the program execution;before, I didn't use cin.ignore(), only cin.get(), and it didn't pause the console at the end. Again, thanks for showing me another usage of cin.ignore() to prevent possible user error, but can you explain the what the arguments mean in cin.ignore(80, '\n');?

  5. #20
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    If you want to be a C++ programmer, then you have to learn to use the C++ documentation. Your homework assignment for today is to look up what the parameters to cin.ignore() mean.

  6. #21
    Registered User
    Join Date
    Jul 2005
    Location
    US
    Posts
    6

    Any Suggestions?

    7stud, do you have any c++ documentation suggestions for Dev-C++? There are so many different types of c++ documentations. I am c++ noob who started to actually type out codes and experiment like a month ago. Thanks for the advices, man!

  7. #22
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    C++ is a standardized language. That means there is a governing body that decides what can and can't be part of the C++ language. Therefore, things like what parameters a function has are published as part of the documentation of the C++ language. What compiler you use is irrelevant.

    However, for various reasons, the compilers the sofware companies distribute are not always in complete compliance with the C++ standards. But, until you know otherwise, you can assume your compiler follows the standards(and it will in almost all cases).

    There is a site called www.google.com that can help you complete todays homework assignment.
    Last edited by 7stud; 07-28-2005 at 06:20 PM.

  8. #23
    Registered User
    Join Date
    Jul 2005
    Location
    US
    Posts
    6

    Talking Homework completed!

    Hey, 7stud! I found out what the arguments mean in cin.ignore() and even getline() too. So, as you stated in your other post, getline() could serve the same function as cin.ignore() and cin >> for better input error prevention or to pause the console. I think getline() is much simpler and shorter in code. Thanks for explaining the details of the functions, getline() and cin >>. They are crystal clear!

  9. #24
    Registered User
    Join Date
    Jul 2005
    Posts
    2

    Similar problem...?

    Hi.

    I am really new to C++ so this may have been answered here but I'm not sure. Here is my simple code:

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        //Ask the user for a number
    	int input_value;
    	cout << "Please enter a number from 1 to 10:";
        cin >> input_value;
    
        if ((input_value >= 1) && (input_value <= 10))
    	{
          //The number is between 1 and 10, so display it and exit
    	  cout << "You entered: " << input_value << "\n";
    	}else{
    	  //The number is wrong. Inform user and restart function
          cout << "You entered: " << input_value << ". Please try again!\n\n";
    	  main();
    	}
      return 0;
    }
    This works fine, except when input that is not of type int is entered. Then it goes into an unending loop - ignoring the cin each time. Why is this?

    Thanks,
    Mike

  10. #25
    Registered User
    Join Date
    Jul 2005
    Posts
    2

    nevermind

    Nevermind! I figuerd it out on my own...

    Just added a cklear and ignore as below:

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        //Ask the user for a number
    	int input_value;
    	cout << "Please enter a number from 1 to 10:";
        cin >> input_value;
    
        if ((input_value >= 1) && (input_value <= 10))
    	{
          //The number is between 1 and 10, so display it and exit
    	  cout << "You entered: " << input_value << "\n";
    	}else{
    	  //The number is wrong. Inform user and restart function
          cout << "You entered: " << input_value << ". Please try again!\n\n";
    	  cin.clear();
    	  cin.ignore();
    	  main();
    	}
      return 0;
    }
    -Mike

  11. #26
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,801
    Do not call main. Use a loop instead.
    Woop?

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem using "cin" in a thread function?
    By Fossil in forum Windows Programming
    Replies: 4
    Last Post: 11-24-2003, 08:08 PM
  2. improving input (replacing "cin")
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 12-07-2001, 04:55 PM

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