Hey, i was thinking these may help me if I made these little programs etc, but i've came to a situation, I've go no error but have to use

cin.get();

twice, here's the whole program..

#include <iostream>
using namespace std;

int main()
{
    system("title Simple use of if else if loops.");
    system("Color C");
    
    int digit;
    
    cout << "Enter a positive digit: ";
    cin >> digit;
    
    if( digit > 0 )
    {
             cout << endl << "Thank you!";
    }
    
    else if( digit < 0 )
    {
         cout << endl << "I said postive!"; 
    }
    
    cin.get();
    cin.get();
    
    return 0;
}

The reason i have to use it twice is that if I only use it once the program just opens and closes straight away, I am not sure but I am certain it is because i have made the program for the user to console input the variable digit, am I correct with my theory?

Use cin.getline() to read all your input, then you don't get caught up with all the left-over characters normal cin >> var leaves behind.

Thanks, i get a couple of errors though, do i need to include a header or something else?

Error's:

C:\Users\ayre\Desktop\Learning C++\forLoop.cpp:18: error: no matching function for call to `std::basic_istream<char, std::char_traits<char> >::getline()'
C:/Dev-Cpp/include/c++/3.4.2/bits/istream.tcc:582: note: candidates are: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::getline(_CharT*, std::streamsize, _CharT) [with _CharT = char, _Traits = std::char_traits<char>]
C:/Dev-Cpp/include/c++/3.4.2/istream:399: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::getline(_CharT*, std::streamsize) [with _CharT = char, _Traits = std::char_traits<char>]

Execution terminated

I tried that but still get errors,

#include <iostream>
using namespace std;

std::string aLine;

int main()
{
    system("title Simple use of the switch statement.");
    system("Color C");
    
    unsigned int digit;
    
    cout << "Please enter a digit between 1 & 5 : ";
    cin >> digit;
    
    switch(digit)
    {
                 case '1': 
                      cout << endl << "You selected 1!";
                           break;
                           
                 case '2': 
                      cout << endl << "You selected 2!";
                           break;
                           
                 case '3': 
                      cout << endl << "You selected 3!";
                           break;
                           
                 case '4': 
                      cout << endl << "You selected 4!";
                           break;
                           
                 case '5': 
                      cout << endl << "You selected 5!";
                           break;
    }
    
    cin.getline( aLine );
    
    return 0;
}

Or have i used the code incorrectly?

Ummm I'm not quite sure if this is what you are looking for but to keep the console open you could just type the code:

sytem("pause");

(i.e. replace cin.getLine(aLine) with that)

EDIT I'll take a look when I get home...busy now

That's what the cin.ingnore() is for. Your previous 'cin' has left a '\n' on the stream. So if you call cin.get() the '\n' will 'come out' of the stream and your program will continue. With cin.ignore(), you ignore this ' \n'

This article has been dead for over six months. Start a new discussion instead.