0

I need to write a program where it asks you to input two integers and then it outputs those same two integers, but if you enter '|', it will end the program.

This is what I have, to me it should work, but unfortunately it doesn't.

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>
#include <iomanip>
using namespace std;

int main()
{
    int var1 = 0;
    int var2 = 0;

    while(1)
    {
        cout << "Please enter two numbers.\n";
        cin >> var1;
        cin >> var2;

        if(var1 == '|')
            break;

        else
        {
            if(var2 == '|')
                break;

            else
            {
                cout << var1 << ' ' << var2 << '\n';
            }
        }       
    }
}

I'm sure it's some simple concept that I'm missing, but any help would obviously be greatly appreciated.

M.M
  • 138,810
  • 21
  • 208
  • 365
Joe
  • 95
  • 1
  • 1
  • 7
  • 2
    `cin >> var1;` will not read anything into `var` when the input starts with `|`. You'll have to change your strategy for processing the input. – R Sahu Sep 24 '14 at 04:39
  • What is your output when you put in, say 2 and |? – Nathan Wride Sep 24 '14 at 04:39
  • You can refer following section http://stackoverflow.com/questions/1116493/how-to-quit-a-c-program – Shemil R Sep 24 '14 at 04:39
  • @NathanWride 'Please enter two numbers. 2 0 Please enter two numbers. 2 0 Please enter two numbers. 2 0 Please enter two numbers. 2 0 Please enter two numbers. 2 0 Please enter two numbers. 2 0' And it constantly repeats it – Joe Sep 24 '14 at 04:42
  • @Joe this is telling you that it is reading 0, or no character as @R Sahu said. Try Cheers and hth alf's answer – Nathan Wride Sep 24 '14 at 04:46

4 Answers4

2

When you read an integer, user input like | will just cause a (silent) error, which places cin in error mode.

Until the error mode is cleared further input operations are then ignored, so you get infinite looping.

Instead read the user input as strings, using std::getline from the <string> header. Check if the input line starts with digit or “|”. If digit, convert to integers using e.g. std::stoi.


The language’s built-in syntax for infinite loop is for(;;). It has the practical advantage that Visual C++ won’t issue a silly-warning about constant condition expression.

Cheers and hth. - Alf
  • 142,714
  • 15
  • 209
  • 331
0

Try using declaring var1 and var2 as char type if it requires int then try

 if(var2 == 124)
Rasel
  • 5,488
  • 3
  • 30
  • 39
0

your var1 and var2 cannot be an integer type, if you want to accept any characters other than numeric number. So you must declare with char either string.

I'm sure you going to need the both number to do some calculating function, so convert var1 and var2 from string to integer type.

refer to this: http://en.cppreference.com/w/cpp/string/basic_string/stol

This answer to your question on "How to end a program"

you can use

exit(1);

example of code:

#include <iostream>
#include <string>
#include <stdlib.h>
using namespace std;

int main()
{
    string var1,var2;
    int num1,num2;

    cout << "Please enter two numbers.\n";
    cin>>var1;

    if(var1 != "|")
    {
        num1 = ::atoi(var1.c_str());
    }
    else
    {
        cout<<"Programs Terminated."<<endl;
        exit(1);
    }

    cin>>var2;

    if(var2 != "|")
    {
        num2 = ::atoi(var2.c_str());
    }
    else
    {
        cout<<"Programs Terminated."<<endl;
        exit(1);
    }

    cout<<"\nSum of 2 number: "<<num1+num2<<endl;
}
J4X
  • 119
  • 14
0

Instead of all the code starting from if(var1 == '|'), do this:

if ( !cin )
    break;

cout << var1 << ' ' << var2 << '\n';

When you use << to read into an int, and if the input does not actually contain an int, then it puts cin into a fail state . Testing !cin checks to see if cin is in the fail state.

To read more about this, read any C++ book or reference.

M.M
  • 138,810
  • 21
  • 208
  • 365