Thread: beginers question about cin

  1. #1
    Registered User
    Join Date
    Apr 2010
    Location
    Vancouver
    Posts
    132

    beginers question about cin

    Why doesn't this program prompt the user to enter a letter and display it?

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
    	char hold = 'J';
    	cout << "enter character: ";
    	cin >> hold;
    	cout << "after cin" << endl;
    	cout << hold << endl;
    	cout << "end of program"<<endl;
    	return 0;
    }
    I keep getting:
    enter character: after cin
    7
    end of program

  2. #2
    Registered User
    Join Date
    Sep 2010
    Posts
    15
    Maybe because you are writing 7 each time that you run the program. This code have nothing wrong. It is doing what it should. If the problem is that you are getting 7 no matter what other character you enter then... try to clean the solution and rebuild it. In the worst case, copy & paste your code to another project. But I really don't see anything wrong. Let me know.

  3. #3
    Registered User
    Join Date
    Sep 2010
    Location
    London
    Posts
    41
    Looks fine to me. Did you manage to get it working?

  4. #4
    Registered User
    Join Date
    Jul 2009
    Posts
    7
    It does display the character that you input into the command line. At least for me, I am using GCC in GNU/Linux.

    I also made a couple modifications of the program. The first one displays the initial hold character as well, and one shows what happens when you don't assign hold anything.
    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
    	char hold = 'J';
    	cout << "before cin" << endl;
    	cout << hold << endl;
    	cout << "enter character: ";
    	cin >> hold;
    	cout << "after cin" << endl;
    	cout << hold << endl;
    	cout << "end of program"<<endl;
    	return 0;
    }
    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
    	char hold;
    	cout << "before cin" << endl;
    	cout << hold << endl;
    	cout << "enter character: ";
    	cin >> hold;
    	cout << "after cin" << endl;
    	cout << hold << endl;
    	cout << "end of program"<<endl;
    	return 0;
    }

  5. #5
    Nasal Demon Xupicor's Avatar
    Join Date
    Sep 2010
    Location
    Poland
    Posts
    179
    Nothing wrong here, GCC 4.5.0 on Windows. The code works fine.

    How do you run it? If you run it like this:
    Code:
    ./myapp < textfile
    then you will get:
    Code:
    enter character: after cin
    N
    end of program
    Where N is the first non-whitespace character of the "textfile". Otherwise, if you won't redirect stream to your app, you will get the same behaviour as we do, as in program waiting for input and displaying its first non-whitespace character.

  6. #6
    Registered User
    Join Date
    Apr 2010
    Location
    Vancouver
    Posts
    132
    Sorry you guys it was working. I was using Eclipse and I made a dumb mistake where I had a Java project open and C++ and when I ran this C++ program the Java program grabbed the attention away before anything could get inputted and that's what screwed things up. I need a tutorial on how to use Eclipse.
    Sorry for the wild goose chase you guys!

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Meh. IMHO, you need to dump Eclipse. Worst. Editor. Ever.
    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.

  8. #8
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Quote Originally Posted by Elysia View Post
    Meh. IMHO, you need to dump Eclipse. Worst. Editor. Ever.
    It was kind of okay compared to the nano/g++ combination I usually use :P. So what would you recommend then, for Linux development?

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I don't know. Code::Blocks maybe?
    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.

  10. #10
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Quote Originally Posted by Elysia View Post
    I don't know. Code::Blocks maybe?
    Fair point. Always thought it was Windows only when I read about it :P.

  11. #11
    Third Eye Babkockdood's Avatar
    Join Date
    Apr 2010
    Posts
    352
    It must be skipping the cin statement because you already assigned hold to 'J'. I don't know why it would write a 7, tho.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. simple cin question
    By verbity in forum C++ Programming
    Replies: 25
    Last Post: 05-09-2007, 03:02 PM
  2. CIN Input count question?
    By kamran in forum C++ Programming
    Replies: 5
    Last Post: 10-24-2006, 04:06 PM
  3. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 09:22 PM
  4. cin Help...Sort of a noob Question?
    By Krak in forum C++ Programming
    Replies: 9
    Last Post: 01-26-2003, 01:23 PM
  5. cin question
    By joebudden in forum C++ Programming
    Replies: 5
    Last Post: 01-07-2003, 07:38 PM