First Program

This is a discussion on First Program within the C++ Programming forums, part of the General Programming Boards category; Hello all, im extremely new to C++ and im currently just trying to practice writing basic programs but i have ...

  1. #1
    <In Learning Mode>
    Join Date
    Dec 2004
    Posts
    6

    Question First Program

    Hello all, im extremely new to C++ and im currently just trying to practice writing basic programs but i have come up with a problem:

    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {	
    	int convert;
    	char letter;
    
    	cout << "Enter a Character: ";
    	cin >> letter; 
    	convert = (int) letter;	
    	cout << letter << " in ASCII is: " << convert << endl;
    	return 0;
    }
    I would like the program to stop exiting the cmd window when it has finished so that i can see the results, ive tried the cin.get(); method but it doesnt seem to work.

    I would also like a new line that seperates "Enter a Character" and "in ASCII is" because they currently show up under one another but i want a blank space in between the two sentences (i hope you understand what i mean), ive tried the "\n" and endl; commands but they dont work

    thanks for any help in these two matters

  2. #2
    Registered User
    Join Date
    Sep 2004
    Posts
    197
    you can use the newline character \n to create a new line.
    If any part of my post is incorrect, please correct me.

    This post is not guarantied to be correct, and is not to be taken as a matter of fact, but of opinion or a guess, unless otherwise noted.

  3. #3
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,062
    Well here is a look at the corrected code.

    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {	
    	int convert;
    	char letter;
    
    	cout << "Enter a Character: ";
    	cin >> letter; 
    	cin.ignore();      // Takes the \n character from the stream
    	convert = (int) letter;	
    	cout << "\n" << letter << " in ASCII is: " << convert;  // Newline at the beginning
    	cin.get();                                              // of this output
    	return 0;
    }
    The \n character is good where I put it, keep in mind this could be replaced with an endl if you wish, though with more output to come, I don't really see the point. The reason cin.get() wasn't working for you is because you still had a newline character in the input stream and it was picking that up and closing. I added a cin.ignore() after the input to take that away for you. You could also have used 2 cin.get() calls at the end if you wish.

    This should work fine. I wish you explained in detail why \n wasn't working for you, I'm just assuming you didn't have it in the right place, though.
    Sent from my iPadŽ

  4. #4
    <In Learning Mode>
    Join Date
    Dec 2004
    Posts
    6
    thankyou so much for your replies, and thankyou SlyMaelstrom for re-writing the program, it now works just the way i wanted it to

    Please could you explain a bit more about why cin.ignore() was required and cin.get() didn't work because i dont understand where i had the newline character in the input stream or what it causes

    I would also like to know about how program code is read by the computer (program\source code flow) so that i can understand how to write code in the correct order because you were right about me not putting the "\n" character in the right place.
    Last edited by $watto; 11-20-2005 at 12:07 PM.

  5. #5
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by $watto
    t
    Please could you explain a bit more about why cin.ignore() was required and cin.get() didn't work because i dont understand where i had the newline character in the input stream or what it causes
    cin >> letter just pulls one character from cin. But you have to actually type a character and a newline to get anything from cin. The newline stays in the buffer. if you call cin.get() afterwards there is still that newline in the buffer and that will get returned. So it looks like it has no effect.
    SlyMaelstrom has put a cin.ignore() after the cin >> letter. this kind of empties the buffer so cin.get() will wait for some more input.
    Kurt

  6. #6
    Rabite SirCrono6's Avatar
    Join Date
    Nov 2003
    Location
    California, US
    Posts
    269
    Just a quick note:

    Code:
    convert = static_cast<int>( letter );
    is usually used with C++, whereas ( int )letter is a C-Style cast.

    - SirCrono6
    From C to shining C++!

    Great graphics, sounds, algorithms, AI, pathfinding, visual effects, cutscenes, etc., etc. do NOT make a good game.
    - Bubba

    IDE and Compiler - Code::Blocks with MinGW
    Operating System - Windows XP Professional x64 Edition

  7. #7
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Please could you explain a bit more about why cin.ignore() was required and cin.get() didn't work because i dont understand where i had the newline character in the input stream or what it causes
    A line like this:

    cin >>num

    waits for the user to enter some input. Then the user can type something in and hit Return. However, when the user hits Return, an invisible \n or "newline" character is entered after the input ('\n' is considered a single character). So, if the user types in 10 and hits Return, the input actually looks like this:

    10\n

    Now, let's examine how that input is handled. cin>> is programmed to skip all leading whitespace(e.g. spaces, tabs, and newlines), read in data, and stop reading in data when the first whitespace character is encountered. And, very importantly, the terminating whitespace character is not read in. So, with this input:

    10\n

    1) cin>> skips any leading whitespace(there's no whitespace in front of 10)
    2) cin>> reads in 10, and
    3) cin>> stops reading when it encounters the whitespace character \n
    4) cin>> does not read in the \n, and leaves the \n in the input stream.

    Sometime thereafter, if you have a line like this:

    cin.get();

    that is an instruction to read in more input. Specifically, cin.get() says to read in one character. In this case, there is still a \n left in the input stream, and as far as cin.get() is concerned that is good input, so it doesn't need to wait for the user to enter data--cin.get() goes ahead and reads in the \n. So, execution continues on without waiting for the user to enter something.

    The line:

    cin.ignore(n);

    will cause n characters in the input stream to be skipped. Since 1 is the default value, you can skip one character by writing:

    cin.ignore();
    Last edited by 7stud; 11-20-2005 at 03:43 PM.

  8. #8
    <In Learning Mode>
    Join Date
    Dec 2004
    Posts
    6
    thanks, i now better undrstand why cin.get() didnt work

    And what about program\source code flow? how does C++ read the source code when it is compiled because i think that is the reason why i placed the newline character in the wrong place because i dont actually know how the c++ compiler deals with the source code [in what order does it process it]

    thanks for your help so far all

  9. #9
    xhi
    xhi is offline
    !anExpert
    Join Date
    Mar 2005
    Location
    pa
    Posts
    155
    Quote Originally Posted by SirCrono6
    Just a quick note:

    Code:
    convert = static_cast<int>( letter );
    is usually used with C++, whereas ( int )letter is a C-Style cast.

    - SirCrono6
    It should be pointed out here that a cast is not needed in the case of chars and ints
    Code:
    int main()
    {
        char in='\0';
        int out=0;
        cin >> in;
        out=in;
        cout << out << endl;
        return 0;
    }

  10. #10
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    It should be pointed out here that a cast is not needed in the case of chars and ints.
    Some books say it is good practice to make casts explicit rather than relying on implicit casts. Explict casts show you intended for the cast to happen.

  11. #11
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    And what about program\source code flow? how does C++ read the source code when it is compiled
    Sequentially, from the top of the file to the bottom of the file and from left to right.
    Last edited by 7stud; 11-20-2005 at 03:53 PM.

  12. #12
    <In Learning Mode>
    Join Date
    Dec 2004
    Posts
    6
    I see what your saying 7stud so why wouldnt this work:

    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {	
    	int convert;
    	char letter;
    
    	cout << "Enter a Character: ";
    	cin >> letter; 
    	convert = (int) letter;	
            cout << "\n"; //doesn't create a newline
    	cout << letter << " in ASCII is: " << convert << endl;
    	return 0;
    }

  13. #13
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,062
    That code should work fine, Swatto. Are you sure it doesn't create a newline for you?
    Sent from my iPadŽ

  14. #14
    <In Learning Mode>
    Join Date
    Dec 2004
    Posts
    6
    hmm strange, yes your right it does work

    didnt work the first time i tried it

    another thing i wanted to ask, if source code is correctly written why do some compilers throw it back at you with errors but other compilers work?

    **Sorry for all these questions, just like to gain knowledge**

  15. #15
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    It depends on the source code, the compiler, and the error. Do you have a specific instance with the specific errors?

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

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 12:38 PM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 12:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 09:39 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM

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