hello, brand new here, need help with inputting data

This is a discussion on hello, brand new here, need help with inputting data within the C++ Programming forums, part of the General Programming Boards category; okay, so i have this basic program (from the tutorials section), this is what it is: Code: #include <iostream.h> int ...

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    8

    hello, brand new here, need help with inputting data

    okay, so i have this basic program (from the tutorials section), this is what it is:


    Code:
    #include <iostream.h>			
    int main()				//Most important part of the program!
    {
      int age;				//Need a variable...
      cout<<"Please input your age: ";	//Asks for age
      cin>>age;				//The input is put in age
      if(age<100)				//If the age is less than 100
      {
         cout<<"You are pretty young!";     //Just to show it works
      }
      else if(age==100)		//I use else just to show an example 
      {
         cout<<"You are old";		//Just to show you it works...
      }
      else
      {
        cout<<"You are really old";	//Executed if no other statement is executed
      }
      return 0;
    }


    anyways, so the problem is, i run the console program and i get prompted :how old are you, so i put in a number and hit enter, then the window just dissappears, this has happened with all my programs that require data inputted.

    any help would be great! thanks

    and BTW, I did do a search, and didn't find anything.
    Last edited by Chrisab508; 10-10-2003 at 10:18 PM.

  2. #2
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    From the FAQ:

    http://faq.cprogramming.com/cgi-bin/...&id=1043284385

    Also, as a general comment, you can improve your commenting style. A comment shouldn't simply restate one line of code. If your code is so obfuscated that you need a comment line to explain every line, then rewrite your code to be more clear.

    And you are using old headers. This is a little better:

    Code:
    #include <iostream>
    using namespace std;
    
    /* main()
    
    Program entry point.  Queries the user for their age, and
    outputs a text message depending on the value they enter.
    
    Parameters: None
    Returns: 0
    */
    
    int main()
    {
      int age;
      cout<<"Please input your age: ";
      cin>>age;
      if(age<100)
      {
         cout<<"You are pretty young!";
      }
      else if(age==100)	
      {
         cout<<"You are old";
      }
      else
      {
        cout<<"You are really old";
      }
      return 0;
    }
    One acceptable use of comments for small sections of code is to name the parameters of a messy function, like WinAPI calls. For example, this:

    Code:
    HANDLE h = CreateFile(
      "c:\\file.txt",  // file name
      GENERIC_READ,    // Access permissions
      0,               // Share mode
      NULL,            // Pointer to security attributes
      OPEN_EXISTING,   // Open vs. create options
      0,               // Flags
      NULL             // Handle to template file
    );
    Good comments:

    * Summarize a function or portion of a function, or
    * Summarize the parameters or returns of a function, or
    * Provide information that otherwise would not be obvious

    In this case, my commented version of your code does the first 2 (it allows you to discern the purpose of the function) and my WinAPI comment serves the third purpose. You could not look at, for example, the Share Mode parameter and know what it was, except for the comment that says it is share mode.
    Last edited by Cat; 10-10-2003 at 10:35 PM.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  3. #3
    Registered User
    Join Date
    Oct 2003
    Posts
    8

    lol

    okay, lol i really didn't understand what you were saying there. So basically ur saying that the code i used is kind of outdated? and that i should use the other one? but whats the difference? and what exactly was my problem that when i hit enter the screen disappeared. I've only been doin this for like 4 hours lol!

    and if the code I was using was outdated, howcome its still in the tutorials sections?
    thanks a lot!

  4. #4
    Registered User
    Join Date
    Aug 2003
    Posts
    1,203
    To make your console window stay you can use this piece of code:

    Code:
    #include <cstdlib>
    
    int main()
    {
    	/////////////////
    	//Code goes here
    	////////////////
    	system("PAUSE");
    	return 0;
    }
    or if that doesnt work:

    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
    	/////////////////
    	//Code goes here
    	////////////////
    	cin.get();
    	return 0;
    }

  5. #5
    Registered User Joe100's Avatar
    Join Date
    Jul 2003
    Posts
    63
    Dude if ur new u should just by a book the one i suggest is C++ For dummies u can not program with out a book dont look of tutorials
    "Be Quick Or Be Dead"

    [img]
    http://www.danasoft.com/sig/GU.jpg
    [/img]

  6. #6
    Registered User
    Join Date
    Oct 2003
    Posts
    8
    okay, well the window does stay there, but whne i input data, and hit enter, then it disappears.

  7. #7
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571

    Re: lol

    Originally posted by Chrisab508


    and if the code I was using was outdated, howcome its still in the tutorials sections?
    thanks a lot!
    Those tutorials are very outdated; they ar pre-1998 C++.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  8. #8
    Registered User
    Join Date
    Aug 2003
    Posts
    1,203
    Originally posted by Chrisab508
    okay, well the window does stay there, but whne i input data, and hit enter, then it disappears.
    ooops sorry my bad, if you tried the second solution I had I forgot to include something, this is the correct one:
    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
        /////////////////
        //Code goes here
        ////////////////
        cin.ignore();
        cin.get();
        return 0;
    }
    And not this one:
    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
        /////////////////
        //Code goes here
        ////////////////
        cin.get();
        return 0;
    }
    Last edited by Shakti; 10-11-2003 at 11:24 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inputting Data File Using Pointers
    By jl864405 in forum C Programming
    Replies: 8
    Last Post: 06-04-2009, 05:52 PM
  2. Lame null append cause buffer to crash
    By cmoo in forum C Programming
    Replies: 8
    Last Post: 12-29-2008, 02:27 AM
  3. simultaneously waiting for data on FIFO and UDP using select call
    By yogesh3073 in forum Networking/Device Communication
    Replies: 2
    Last Post: 01-05-2007, 08:53 AM
  4. Where's the EPIPE signal?
    By marc.andrysco in forum Networking/Device Communication
    Replies: 0
    Last Post: 12-23-2006, 07:04 PM
  5. Program Crashing
    By Pressure in forum C Programming
    Replies: 3
    Last Post: 04-18-2005, 10:28 PM

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