I know, dumb question but...

This is a discussion on I know, dumb question but... within the C++ Programming forums, part of the General Programming Boards category; I'm new to C++, but not to programming in general (know a bit of Java). I finally found a decent ...

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    12

    I know, dumb question but...

    I'm new to C++, but not to programming in general (know a bit of Java). I finally found a decent compiler (Dev cpp) and got to work on a program that adds a couple of numbers together. Everything works fine until I press return after entering a couple of numbers;this gets rid of the console window before the calculation is made and appears on screen. Now before I post code, I want to make sure that this isn't something compiler related, or maybe something with my return statement. I assume if the code is correct, the console window should add the numbers, display the answer, and just I am sillyI am sillyI am sillyI am silly there 'till I press a key, right? Any ideas?

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,337
    I dunno - perhaps you could read the FAQ
    http://faq.cprogramming.com/cgi-bin/...&id=1043284385
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    i dont know Vicious's Avatar
    Join Date
    May 2002
    Posts
    1,200
    use the cin.get method at the end of your program.

    Whats happens is that the program gets done and exits.

    cin.get waits for the user to press enter, so add that right before return 0;

    hopefully it looks like so

    Code:
    #include <iostream> //+---Standard Header---+
    
    int main()
    {
    
        //+--Your code---+
    
        std::cin.get();   //+---Wait for enter---+
        return 0;
    
    }

  4. #4
    Registered User
    Join Date
    Sep 2004
    Posts
    12
    Thanks for the replies so far. That helps keep the console window up so I can enter the numbers, but when I enter them and press enter (which calculates the sum of the numbers) the console screen disappears. Here's my code...

    #include <iostream.h>

    int main()
    {
    int a,b,c,sum;

    cout <<"\nWelcome. This program adds\n";
    cout <<"three numbers. Enter three numbers\n";

    cin >>a>>b>>c;

    sum = a+b+c;

    cout <<"The sum is: " << sum << "\n";



    cin.get();
    return 0;
    }
    I actually just copied this from a textbook to test it out, it's obvious the program is functional, but the dang console window leaves when it's about to calculate the answer.

  5. #5
    Registered User
    Join Date
    Aug 2004
    Posts
    731
    I always use #include <conio.h> and then use getch() as my pause.

  6. #6
    i dont know Vicious's Avatar
    Join Date
    May 2002
    Posts
    1,200
    <iostream.h> is no longer part of standard C++

    add this in-place of iostream.h

    Code:
    #include <iostream>
    using namespace std;
    cin >>a>>b>>c;

    that doesnt look good

    try this

    Code:
    cin >> a;
    cin >> b;
    cin >> c;
    if you want them to be entered in one line, use cin.getline() or cin.get() method.

    Also conio.h is not standard either. Some compilers dont even support it.

    Just to explain,
    cin and cout are part of the std namespace.

    Code:
    using namespace std;
    ^^ that is saying that you want to use the std namespace globally. If you dont want global declaration of the namespace you can add std:: to cin and cout.

    Code:
    #include <iostream>
    
    int main()
    {
    
        std::cout << "blah, blah..";
    
        std::cin.get();
        return 0;
    
    }
    Last edited by Vicious; 09-08-2004 at 04:48 PM.

  7. #7
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,600
    read this.
    Code:
    if( numeric_limits< byte >::digits != bits_per_byte )
        error( "program requires bits_per_byte-bit bytes" );
    24bbs.cpp

  8. #8
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    Sebastiani's link looks like it has some good information in it.. for quick reference here is how you can fix the specific problem:
    Code:
     #include <iostream.h>
    
    int main()
    {
    int a,b,c,sum;
    
    cout <<"\nWelcome. This program adds\n";
    cout <<"three numbers. Enter three numbers\n";
    
    cin >>a>>b>>c;
    
    sum = a+b+c;
    
    cout <<"The sum is: " << sum << "\n";
    
    
    cin.ignore();
    cin.get();
    return 0;
    }
    The reason is that when you extract a, b, and c from the input stream, the newline from when you hit enter is still there. The cin.get() waits for the next newline, but since there is already one there your console closes. The call to cin.ignore() above fixes that specific problem by ignoring that first newline and allowing the cin.get() to wait for the next one. You could also put cin.get() twice or cin.ignore() twice to achieve the same effect.

    I'd prefer this code, because making that simple change above still leaves many potential problems.
    Code:
    #include <iostream>
    #include <limits>
    
    int main()
    {
        int a,b,c,sum;
    
        std::cout <<"\nWelcome. This program adds\n";
        std::cout <<"three numbers. Enter three numbers\n";
    
        std::cin >> a >> b >> c;
    
        sum = a+b+c;
    
        std::cout <<"The sum is: " << sum << "\n";
    
        std::cin.ignore(std::numeric_limits<int>::max(), '\n');
        std::cin.get();
        return 0;
    }
    That uses standard headers, and it ignores all characters in the input stream - which helps in case the user accidentally types in characters after the last number. There are several other error-prrofing things you could do, but that is a start.

  9. #9
    Registered User
    Join Date
    Sep 2004
    Posts
    12
    Alrighty, everything worked as it should after throwing in the second cin.get() line and switching to namespace std, thanks a ton for the help. For confirmation, I've got one last question on this line

    Code:
    cin.ignore(std::numeric_limits<int>::max(), '\n'
    The \n in single quotes at the end of the line is what it ignores. So I assume it only ignores the very last \n in the code?

  10. #10
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    The '\n' is how it knows when to stop. It reads and ignores everything until it finds that character or until there is nothing left in the stream. So if there were more than one newline in the stream, it will only ignore up to and including the first one.

    In your case, there is only one newline in the stream at the time, because you (the user) hit enter only once since the last input that was read in. Any other newlines before that were skipped when reading in other input. So if you entered:

    3<enter>5<enter>10<enter>

    The 3 would be read into a, the first <enter> would be skipped, the 5 would be read into b, the second <enter> would be skipped and the 10 would be read into c. Then the last <enter> would be ignored by the call to ignore(), and the cin.get() would wait for the user to hit enter one more time.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. dumb question
    By travis999 in forum C Programming
    Replies: 3
    Last Post: 10-26-2007, 12:57 AM
  2. very dumb question.
    By Blips in forum C++ Programming
    Replies: 14
    Last Post: 11-08-2005, 08:37 AM
  3. Dumb question
    By dragon2309 in forum C Programming
    Replies: 18
    Last Post: 10-29-2005, 03:27 PM
  4. Alice....
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 06-20-2005, 02:51 PM
  5. another dumb question - loop not working?
    By Captain Penguin in forum C++ Programming
    Replies: 8
    Last Post: 10-06-2002, 10:15 PM

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