Thread: New to C++, probably an easy question.

  1. #1
    Registered User
    Join Date
    Feb 2004
    Posts
    3

    New to C++, probably an easy question.

    Hi,
    Sorry for the bad thread name, I couldnt think what to put

    I only started programming in C++ today, so please point out anything which I am doing wrong - I don't want to get bad habits this early on

    I have made this program, which asks you for a number, then adds 5 to it and displays the result. Then waits for keypress before terminating:

    Code:
    #include <iostream.h>
    #include <conio.h>
    
    int main ()
    {
      int a;
      int result;
      int numtoadd;
    
      cout << "Number to add 5 to\n";
      cin >> numtoadd;
      a = 5;
      result = numtoadd + a;
    
      cout << result;
    
      cout << "\nThat is the answer to " << numtoadd << " plus 5\n\n";
      cout << "Press any key to continue...\n";
      getch();
      cout<<"A key was pressed... exiting the program";
      return 0;
    }
    The problem I have is, the program seems to wait for keypress before it displays the result and the following text. Could anyone please point out where I have gone wrong?
    Thanks very much for any help

  2. #2
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Instead of doing this:
    Code:
    cout<<"whatever\n";
    do this:
    Code:
    cout<<"whatever"<<endl;
    endl makes sure that the text will be displayed.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  3. #3
    Registered User
    Join Date
    Feb 2004
    Posts
    8
    I don't know what you should do about that problem. I generally use cin.get(); to wait for a keypress. Perhaps your getch() is the problem? I don't know about that because I am also new. However, and this is just a suggestion, I would code like this :

    Code:
    #include <iostream>
    using namespace std;
    int main () {
          int numbertoaddto;
          int answer;
          cout<<"Number to add 5 to?";
          cin >> numbertoaddto;
          answer = numbertoaddto + 5;
          cout<<"Number entered plus 5...";
          cout<<answer;
         cin.get(); 
         cin.get();
          return 0;
          }
    The reason for the two cin.get()'s is because when you enter a number you must hit enter for the number to be processed...this closes out the program because it is waiting for that same keypress to end....Perhaps that is the answer to your problem. Try adding another getch(); and then see if it works.

    Also, you declared int a; and then gave variable A a value of 5. This can also be done by simply adding 5 in an equation as above. Hope that helps!

    Good luck programming!

  4. #4
    Registered User
    Join Date
    Feb 2004
    Posts
    3
    Thanks very much C++programming and XSquared, I tried your suggestions and now it works perfectly

    Again, thanks

    C++Programming, what is:
    using namespace std;

    Thanks very much

  5. #5
    Rabite SirCrono6's Avatar
    Join Date
    Nov 2003
    Location
    California, US
    Posts
    269
    > #include <iostream.h>

    That is not the standard header. It should be:
    #include <iostream>

    > #include <conio.h>

    Warning! That is not a standard header either. You got getch() from it, and getch() is not a standard thing. You shoudn't use it. One way to pause the program before ending is:

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
      //Code
      cin.get(); //Pause
      return 0; //End
    }
    > using namespace std; (See above)

    This is needed. There are several std things. There are also other ways instead of using namespace std.

    Code:
    //#includes
    
    using std::cout;
    using std::endl;
    using std::cin;  //There are more, but these are commonly used ones
    
    //and another way
    
    //Code
    int X;
    std::cout << "What is X?" << std::endl;
    std::cin >> X;
    //Code
    Leaving your code looking something like this:

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
      int Num, Answer;
      cout << "Enter a number." << endl;
      cin >> Num;
      Answer = Num + 5;
      cout << "That number plus five is: " << Answer << endl;
      cin.get();
      return 0;
    }
    - SirCrono6
    Last edited by SirCrono6; 02-25-2004 at 06:33 PM.
    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

  6. #6
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    Originally posted by SirCrono6
    > #include <conio.h>

    Warning! That is not a standard header either. You got getch() from it, and getch() is not a standard thing. You shoudn't use it.
    Shouldn't is a little strong, IMHO. If you are programming in Borland or MS C++ -- and no plans for other compilers, there is nothing wrong with using getch(). BUT you must keep in mind that most compilers have not defined getch(), so you can't use it elsewhere.

    But, use it sparingly. If you don't need to do the single keypress thing (and it's rarely really needed), use a standard technique. If you need that technique though, just remember your code is not portable.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  7. #7
    Registered User
    Join Date
    Feb 2004
    Posts
    3
    SirCrono6, thanks, I see what that means now

    WaltP, I am using MS C++, but this code is just for practiving, more for getting used to the C+ syntax and simple operations than doing anything usefull. But I will keep that in mind for when I start anything that may be portable

    Thanks everyone for the help
    Much appreciated.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. easy Vector contructor question
    By noodle24 in forum C++ Programming
    Replies: 5
    Last Post: 04-21-2006, 07:43 PM
  2. Another Embarassingly Easy Question
    By almo89 in forum C Programming
    Replies: 2
    Last Post: 02-11-2006, 04:59 PM
  3. This is hopefully an extremely easy question...
    By rachaelvictoria in forum C Programming
    Replies: 2
    Last Post: 11-07-2005, 01:36 AM
  4. 4 easy question
    By Zeratulsdomain in forum C++ Programming
    Replies: 2
    Last Post: 10-15-2005, 10:43 PM
  5. Easy Question
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 08-12-2002, 12:19 PM