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.

5
  • 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. Commented Sep 24, 2014 at 4:39
  • What is your output when you put in, say 2 and |? Commented Sep 24, 2014 at 4:39
  • You can refer following section stackoverflow.com/questions/1116493/how-to-quit-a-c-program Commented Sep 24, 2014 at 4: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 Commented Sep 24, 2014 at 4: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 Commented Sep 24, 2014 at 4:46

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

0

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

 if(var2 == 124) 

2 Comments

I'm required to have them set as int's
Then you can use ASCII code of char '|' in comparision
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; } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.