How to check user input? - c++

SO I want to be able to invalidate all user input except a certain word, like 'K' or 'C'. I'm not sure at all how to do this. So if they mispell it to "celcius" or "husdhfjae", my program would say "Input invalid, please enter K or C."
Please nothing too complicated, because I just started. Thank you :)
// CS 575,HW #1B, Ravela Smyth
// This program converts from Fahrenheit to Celsius or Kelvin
#include <iostream>
#include <string>
using namespace std;
int main() {
string input;
double Fahrenheit, celsius, kelvin;
cout << "Hi! What is the weather today in Fahrenheit?? " << endl;
cin >> Fahrenheit;
cout << "Would you like to convert this temperature to Celsius or Kelvin? (C/K)" << endl;
cin >> input;
if (input == "C")
{
celsius = (5 * (Fahrenheit - 32)) / 9;
cout << "Today's weather in Celsius is " << celsius << " degrees! " << endl;
}
else if (input == "c")
{
celsius = (5 * (Fahrenheit - 32)) / 9;
cout << "Today's weather in Celsius is " << celsius << " degrees! " << endl;
}
else if (input == "K")
{
kelvin = (5 * (Fahrenheit + 459.67)) / 9;
cout << "Today's weather in Kelvin is " << kelvin << " degrees!" << endl;
}
else if (input == "k")
{
kelvin = (5 * (Fahrenheit + 459.67)) / 9;
cout << "Today's weather in Kelvin is " << kelvin << " degrees!" << endl;
}
return 0;
}

Usually user inputs are checked using while or do...while loops.
The idea is simple, you always get back to the same error message and read again the input until it is correct.
The advantage of placing the valid options in the single string is to allow easy addition or removal of the options without dealing with long if conditions.
I believe something simple like this will do the job:
std::string valid_options("kKcC");
std::string input;
bool illegal_input;
std::cout << "Would you like to convert this temperature to Celsius or Kelvin? (C/K)" << std::endl;
std::cin >> input;
// check that only one letter was provided and it belongs to the valid options
while (input.size() != 1 || valid_options.find(input) == std::string::npos)
{
std::cout << "Input invalid, please enter K or C.\n";
std::cin >> input;
}

First, you can do something like if(input == "C" || input == "c")
Or you can convert the input to lower/upper case
Second, you can add an else statement that says something like "please enter a valid command". Play around with it, you can even use loops to wait for correct input!

My approach is to test the input against a container of all valid inputs.
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
bool valid(std::string s,std::vector<std::string> y)
{
std::sort(y.begin(), y.end());
return std::binary_search(y.begin(), y.end(), s);
}
int main()
{
std::string s;
do
{
std::cout << "Enter K or C: ";
std::cin >> s;
} while (!valid(s, { "K","C","k","c" }));
std::cout << "good!" << std::endl;
return 0;
}

You need a while loop. This is probably the simplest way to do it.
#include <iostream>
#include <string>
int main()
{
std::string word;
std::cin >> word;
//Keep asking for a word until this condition is false, i.e.
//word will be equal to one of these letters
while(word != "C" && word != "c" && word != "K" && word != "k")
{
std::cout << "Invalid temperature type: " << word << " Use 'K' or 'C'" << std::endl;
std::cin >> word;
}
if (word == "C" || word == "c")
{
std::cout << "Celsius" << std::endl;
}
else if (word == "K" || word == "k")
{
std::cout << "Kelvin" << std::endl;
}
}

I had the same problem while getting the correct user input for that i wrote a simple solution i hope it will be helpfull for everyone getting started with c++.
//get user input
char input;
cin >> input;
//convert the input to lowercase
char Temp = tolower(input);
//check the input (not valid input will clear the user input)
while(!(cin >> input) || ((Temp != 'c') &&( Temp != 'k')){
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << "Invalid input. Please, try again: ";
}
//evalute input cases
switch (Temp)
{
case 'c':
/* celcius */
break;
case 'k':
/* Kelvin */
break;
}

Related

Attempt at making a currency converter but it doesn't start at all

This is my final project for the semester. When I run it on replit, the program runs but doesn't work fully after the first step. When I run it on VS Code, it gives me a linker command error. This is due in 3 days, please any help is appreciated. Thank you
Below is my code:
`
#include <iostream>
#include <cstdlib>
#include <iomanip>
#include <cmath>
#include <string>
using namespace std;
const int HEAD_SPACE = 23;
const int SECND_HDSPACE = 35;
const int THRD_HDSPACE = 27;
char startKey;
char replayKey;
int userChoice;
int main()
{
float conversion(void);
firstStart:
cout << "=======================================================\n\n";
cout << setw(SECND_HDSPACE) << "\t\t\t ::: CURRENCY CONVERTER :::\n\n";
cout << "=======================================================\n\n";
cout << setw(SECND_HDSPACE) << "\t\t\tPlease select any options below\n\n";
cout << setw(SECND_HDSPACE) << "\t\t\t1) Press [1] to convert from USD to GBP\n";
cout << setw(SECND_HDSPACE) << "\t\t\t2) Press [2] to convert from USD to EUR\n";
cout << setw(SECND_HDSPACE) << "\t\t\t3) Press [3] to convert from USD to NZD\n";
cout << setw(SECND_HDSPACE) << "\t\t\t4) Press [4] to convert from USD to JPY\n";
cout << setw(SECND_HDSPACE) << "\t\t\n\n :: PRESS [S] TO START PROGRAM :: \n\n";
menuOption:
cin >> startKey;
if(startKey == 's' && startKey == 'S')
{
float resultVal = conversion();
cout << "The conversion is: " << resultVal << endl;
cout << "Would you like to run the program again? Press Y or N" << endl;
scndStrt:
cin >> replayKey;
if (replayKey == 'y' && replayKey == 'Y')
{
goto firstStart;
}
else if ((replayKey == 'n' && replayKey == 'N'))
{
cout << "Thanks for using my program!" << endl;
}
else
{
cout << "Wrong Key Entered. Please hit Y or N" << endl;
goto scndStrt;
}
}
else
{
cout << "Wrong Key Entered. Please Hit [S]";
goto menuOption;
}
float convervion(void);
int selection;
int currChoice;
float currency1;
float currency2;
cout << "Please enter the currency name: " << endl;
cin >> selection;
cout << "Please enter the amount you would like to convert: " << endl;
cin >> currency1;
switch (selection)
{
case '1':
formulaInput:
cout << "Please enter what currency you would like to conver in: " << endl;
cin >> currency2;
if (currency2 == '1')
{
currency2 = currency1 * 1;
}
else if (currency2 == '2')
{
currency2 = currency1 * 0.84;
}
else if (currency2 == '3')
{
currency2 = currency1 * 0.94;
}
else if (currency2 == '4')
{
currency2 = currency1 * 1.92;
}
else
{
cout << "Incorrect input, please try again." << endl;
goto formulaInput;
}
}
}
`
I tried to watch some videos but the terminology and syntax seemed too advanced for me. I'm only a first year CS student and this is my first semester.
First of all, this program will not run correctly. But beside of that and to your question:
You have to close the main and open the conversion function(s). Check all your curly braces to be correctly.
Your function declaration is inside the main function. As retired Ninja in the comment section said, it is not incorrect and you can do this. Please think about local and global scope. (Within the main function it will become local scope for main and outside the declaration would be in global scope)
But if you think about it, if you wold define another function beneath the in local scope declared function, the same function will become global scope anyway. So for this reason it would be preferable to declare the function in global scope directly. For now, just from the standpoint, to avoid unnecessary irritation.
You have a misspell in your functions declaration and definition. (conversion / convervion ) please check this.
Your function is declared and defined with a return value of float but you forget the final return statement, which you will need to print out the calculation result.
.
#include <iostream> // Do you realy need all of these includes?
#include <cstdlib>
#include <iomanip>
#include <cmath>
#include <string>
using namespace std;
const int HEAD_SPACE = 23;
const int SECND_HDSPACE = 35;
const int THRD_HDSPACE = 27;
char startKey;
char replayKey;
int userChoice;
float conversion(void);
int main()
{
firstStart:
cout << "=======================================================\n\n";
cout << setw(SECND_HDSPACE) << "\t\t\t ::: CURRENCY CONVERTER :::\n\n";
cout << "=======================================================\n\n";
cout << setw(SECND_HDSPACE) << "\t\t\tPlease select any options below\n\n";
cout << setw(SECND_HDSPACE) << "\t\t\t1) Press [1] to convert from USD to GBP\n";
cout << setw(SECND_HDSPACE) << "\t\t\t2) Press [2] to convert from USD to EUR\n";
cout << setw(SECND_HDSPACE) << "\t\t\t3) Press [3] to convert from USD to NZD\n";
cout << setw(SECND_HDSPACE) << "\t\t\t4) Press [4] to convert from USD to JPY\n";
cout << setw(SECND_HDSPACE) << "\t\t\n\n :: PRESS [S] TO START PROGRAM :: \n\n";
menuOption:
cin >> startKey;
if (startKey == 's' && startKey == 'S')
{
float resultVal = conversion();
cout << "The conversion is: " << resultVal << endl;
cout << "Would you like to run the program again? Press Y or N" << endl;
scndStrt:
cin >> replayKey;
if (replayKey == 'y' && replayKey == 'Y')
{
goto firstStart;
}
else if ((replayKey == 'n' && replayKey == 'N'))
{
cout << "Thanks for using my program!" << endl;
}
else
{
cout << "Wrong Key Entered. Please hit Y or N" << endl;
goto scndStrt;
}
}
else
{
cout << "Wrong Key Entered. Please Hit [S]";
goto menuOption;
}
}
float conversion()
{
int selection;
int currChoice;
float currency1;
float currency2;
cout << "Please enter the currency name: " << endl;
cin >> selection;
cout << "Please enter the amount you would like to convert: " << endl;
cin >> currency1;
switch (selection) // ??
{
case '1':
formulaInput:
cout << "Please enter what currency you would like to conver in: " << endl;
cin >> currency2; // This is not working.
if (currency2 == '1')
{
currency2 = currency1 * 1;
}
else if (currency2 == '2')
{
currency2 = currency1 * 0.84;
}
else if (currency2 == '3')
{
currency2 = currency1 * 0.94;
}
else if (currency2 == '4')
{
currency2 = currency1 * 1.92;
}
else
{
cout << "Incorrect input, please try again." << endl;
goto formulaInput;
}
}
// Here you have to return something?
return currency2;
}

Why does This program have a logical error

this is the code i wrote for simple grading exams (im still a very beginner) but when i do a wrong input in (Grades) it doesnt go to the function i made which is called (FalseInput) to make the user able to re-enter the (Grades) any suggestions to how to solve?
and how to improve in general ?
here is an example of whats the problem :
Please Type Your Name : rafeeq
Please Insert The Grade : as (which is an input error)
you failed
thanks.
#include <iostream>
#include <string>
using namespace std;
char Name[30];
int Grades;
const int MinGrade(50);
void FalseInput() {
cout << "pleae enter the number again : ";
cin >> Grades;
if (Grades >= MinGrade) {
cout << Name << " : " << "you passed\n";
cout << Grades;
} else if (Grades < MinGrade and cin.fail() == 0) {
cout << "you failed\n";
} else if (cin.fail() == 1) {
cout << "its not a valid number\n";
cin.clear();
cin.ignore(1000, '\n');
cout << endl;
FalseInput();
}
}
int main() {
cout << "Please Type Your Name : ";
cin.getline(Name, 30);
cout << "Please Insert The Grade : ";
cin >> Grades;
if (Grades >= MinGrade) {
cout << Name << " : " << "you passed\n";
cout << "The Grade Achieved : " << Grades << "%";
} else if (Grades < MinGrade) {
cout << "you failed\n";
} else if (cin.fail() == 1) {
cout << "its not a valid number\n";
cin.clear();
cin.ignore(1000, '\n');
cout << endl;
FalseInput();
}
return 0;
}
You don't check if the extraction of an int succeeds here:
cin >> Grades;
You can check the state of the input stream after extraction like this and it needs to be the first condition or else the program will make the comparisons with MinGrade first and will get a true on Grades < MinGrade.
if(!(cin >> Grades)) {
if(cin.eof()) {
// You can't recover the input steam from eof so here you need
// to handle that. Perhaps by terminating the program.
}
cin.clear();
cin.ignore(1000, '\n');
cout << endl;
FalseInput();
} else if(Grades >= MinGrade) {
cout << Name << " : " << "you passed\n";
cout << "The Grade Achieved : " << Grades << "%";
} else if(Grades < MinGrade) {
cout << "you failed\n";
}
You do have a lot of unnecessary code duplication and you also use an array of char to read the name - but you have included <string> so I assume you're familiar with std::string. I suggest using that.
Simplification:
#include <iostream>
#include <limits>
#include <string>
int main() {
const int MinGrade = 50;
std::string Name;
int Grades;
std::cout << "Please Type Your Name : ";
if(std::getline(std::cin, Name)) {
while(true) {
std::cout << "Please Insert The Grade : ";
if(!(std::cin >> Grades)) {
if(std::cin.eof()) {
std::cout << "Bye bye\n";
break;
}
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
std::cout << "That's not a valid number!\nPlease enter the "
"number again!\n";
} else if(Grades >= MinGrade) {
std::cout << Name << " : " << "you passed\n";
std::cout << "The Grade Achieved : " << Grades << "%\n";
break;
} else { // no need to check "Grades < MinGrade" here
std::cout << "you failed\n";
break;
}
}
}
}
What is happening: When that string "as" is attempted to write to the integer Grades, cin.fail() is set and Grades has the default of 0 written to it (I think that's right)
C++ cin reading string into int type returns 0
All-in-All: Input validation is needed BEFORE you check it's values.
Here is one approach, check if cin was able to successfully convert:
https://www.hackerearth.com/practice/notes/validating-user-input-in-c/
Another approach would be to read cin into a string instead of int, then you can control how to convert/cast it to whatever form you want (more work, but being that you are new - you will learn a lot doing this).
Secondary Note: Your string of 50 characters for name - imagine what would happen if you wrote 60 characters of input. There will be more data than the container can hold, thus writing past the bounds and into his neighbor (could cause a segment fault, or worse - crazy unexpected behavior)
Why does this work? Using cin to read to a char array smaller than given input

Why do I have this infinite loop

hey guys kind of new to the programming world and I know you should read in all input as a string but this is just a simple program and I'm having a brain fart I think but here is my question......why when I press q to quit am i getting an infinite loop and how would I condense the while loops because that looks gross
here is what I have so far
#include "stdafx.h"
#include <iostream>
using namespace std;
int main()
{
int grade;
char quit = 'a';
cout << "Input your grade (0-100): ";
cout << endl;
cin >> grade;
while (grade < 0) {
cout << "If you have a negetive grade....drop out! otherwise enter another grade" << endl;
cin >> grade;
}
while (quit != 'q') {
while (grade < 0) {
cout << "If you have a negetive grade....drop out! otherwise enter another grade" << endl;
cin >> grade;
}
if (grade == 100) {
cout << "You got a perfect grade!" << endl;
cout << "Letter grade: A" << endl;
}
else if (grade >= 90 && grade <= 100) {
cout << "Letter grade: A" << endl << endl;
}
else if (grade >= 80 && grade <= 89) {
cout << "Letter grade: B" << endl << endl;
}
else if (grade >= 70 && grade <= 79) {
cout << "Letter grade: C" << endl << endl;
}
else if (grade >= 60 && grade <= 69) {
cout << "Letter grade: D" << endl << endl;
}
else if (grade < 60) {
cout << "Letter grade: F" << endl << endl;
}
else {
cout << "Invalid grade!" << endl;
}
cout << " would you like to enter another grade? or press q to quit" << endl;
cin >> grade;
}
system("pause");
return 0;`enter code here`
}
Because of grade var. You declared grade as int.
If you type correct int, it works well, but If you type another char ex:) q or f, function cin cannot recognize q or f as int type.
If char input, cin pass own process.
You have to change grade type into char to recognize char and int inputs both.
If you want to use only one input flow, this implementation code will help you.
int _tmain(int argc, _TCHAR* argv[])
{
char c_input[32] = {0};
cin>>c_input;
while(atoi(c_input) < 0)
{
cout<<"If you have a negative grade....drop out! otherwise enter another grade" << endl;
cin>>c_input;
}
while(c_input[0] != 'q')
{
while(atoi(c_input) < 0)
{
cout<<"If you have a negative grade....drop out! otherwise enter another grade" << endl;
cin>>c_input;
}
cout<<c_input;
cout<<"Would you like to enter another grade? or press q to quit" << endl;
cin>>c_input;
}
return 0;
}
Minimal, verifiable example:
#include "stdafx.h"
#include <iostream>
using namespace std;
int main()
{
int grade;
char quit = 'a';
cout << "Input your grade (0-100): ";
cout << endl;
cin >> grade;
while (quit != 'q') {
cout << " would you like to enter another grade? or press q to quit" << endl;
cin >> grade;
}
system("pause");
return 0;`enter code here`
}
See the problem with quit?
edit
What I have done is remove (most of) the lines that have nothing to do with quit or the loop.
At this point you should notice that the loop never changes quit.
If you are having trouble with a program, one of the best ways to figure out what is wrong is to get rid of everything that doesn't have anything to do with the error. In time, you'll be able to do this using only your mind. Duuude!
while i'm at it
The correct way to handle user input is to get it as a string, then convert it to what you want.
For example:
#include <iostream>
#include <sstream>
#include <stdexcept>
#include <string>
template <typename T>
T string_to( const std::string& s )
{
T value;
std::istringstream ss( s );
ss >> value >> std::ws;
if (!ss.eof()) throw std::invalid_argument("T string_to()");
return value;
}
int main()
{
std::cout << "Enter a number or 'q': ";
std::string s;
getline( std::cin, s );
if (s == 'q')
{
std::cout << "Good job! You entered 'q'.\n";
}
else
{
try
{
double x = string_to <double> ( s );
std::cout << "Good job! You entered '" << x << "'.\n";
}
catch (const std::exception& e)
{
std::cout << "Foo, you didn't obey instructions and made me " << e.what() << ".\n";
}
}
}

Fahrenheit to Celsius converter. Verify int not char

Created a program to convert Fahrenheit to Celsius. Having trouble verifying if what the user input is an int. The problem I believe is the "if (!cin)" line.
#include <iomanip>
#include <iostream>
using namespace std;
int main()
{
float f, c;
string choice;
do{
cout << "Enter a Fahrenheit temperature to convert to Celsius:" << endl;
cin >> f ;
if ( !cin ) {
cout << "That is not a number..." << endl;
}
else if (f < -459.67) {
cout << "That is not a Fahrenheit temperature..." << endl;
}
if ( f >= -459.67) {
c = (( f - 32) * 5.0)/9.0 ;
cout << fixed ;
cout << setprecision(2) << "Celsius temperature is: " << showpos << c << endl;
}
cout << "Would you like to convert another? If so, enter Yes" << endl;
cin >> choice ;
}while ( choice == "Yes" || choice == "yes" );
return 0;
}
I'm not sure of what you mean about your "Continue yes or no statement", So I've written a code that asks the user to type yes to confirm the conversion, otherwise no to enter a new Fahrenheit value. After the conversion, The program also asks the user to type yes if he/she wants another conversion, If the user types anything except "yes", The program will close.
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
int main()
{
bool Continue = true;
while (Continue == true)
{
double f, c;
cout << endl << "Enter a Fahrenheit temperature to convert to Celsius:" << endl << endl;
while (!(cin >> f))
{
cout << endl << "Invalid Input. " << endl << endl;
cin.clear();
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
while (f <= -459.67)
{
cout << "Invalid Input. " << endl;
cin.clear();
cin.ignore();
cin >> f;
}
cout << endl << "Continue? Type yes to proceed conversion, Otherwise type no." << endl << endl;
string confirmation;
cin >> confirmation;
while (confirmation != "yes" && confirmation != "no")
{
cout << endl << "Input not recognized. try again." << endl << endl;
cin >> confirmation;
}
if (confirmation == "yes")
{
c = ((f - 32) * 5.0) / 9.0;
cout << fixed;
cout << endl << setprecision(2) << "Celsius temperature is: " << showpos << c << endl;
cout << endl << "Another convertion? type yes to confirm. " << endl << endl;
string cont;
cin >> cont;
if (cont != "yes")
{
Continue = false;
}
}
}
return 0;
}
You should use while loop, So the program won't stop asking until the user enters the correct data. Use cin.clear(); to clear invalid data that the user inputted. And cin.ignore(); to ignore any succeeding erroneous data. For example, '25tg', 'tg' character is ignored since it's not valid. '25' will be accepted.
Edits in my answer and code provided are very welcome.
you can use a function to check if the input is numeric or not....
should be something like this:
bool isFloat( string myString ) {
std::istringstream iss(myString);
float f;
iss >> noskipws >> f; // noskipws considers leading whitespace invalid
// Check the entire string was consumed and if either failbit or badbit is set
return iss.eof() && !iss.fail();
}

Make user give input until they enter X

I'm writing a program in C++ that takes integers from the user until they press "x" to stop.
Then the program will print the number of positives, negatives and zeros.
But whenever the user inputs "x", the program goes into an infinite loop.
I tried removing the "ZEROS" part and just made counters for positives and negatives and it worked good. But I want to count the zeros.
I need to let the user enter numbers including 0 until they enter character x.
Here is my code:
#include <iostream>
using namespace std;
int main() {
int input, neg = 0, pos = 0, zer = 0;
char z;
do {
cout << "Input another positive/negative number or 'x' to stop\n";
cin >> input;
cin.ignore();
if (input > 0){
pos++;
} else if (input == 0){
zer++;
} else if(input < 0){
neg++;
}
} while (z!='x');
cout << "You entered " << pos << " positive numbers.\n";
cout << "You entered " << neg << " negative numbers.\n";
cout << "You entered " << zer << "Zeros.";
return 0;
}
By far the simplest way of getting numbers until a user enters something else is this:
int input = 0;
cout << "Input a positive/negative number or 'x' to stop\n";
while(cin >> input) {
//they entered a number, do stuff
if (input > 0)
pos++;
else if (input == 0)
zer++;
else if (input < 0)
neg++;
cout << "Input another positive/negative number or 'x' to stop\n";
}
//cin failed to read a number, probably because they entered a letter
//if they failed to enter a number, we need to clear the fail flag before we can use cin again
cin.setstate(cin.rdstate()&~std::ios_base::failbit);
cout << "You entered " << pos << " positive numbers.\n";
cout << "You entered " << neg << " negative numbers.\n";
cout << "You entered " << zer << "Zeros.";
I wouldn't recommend anything more complicated until you get very advanced with C++. Parsing input is immensely difficult to get correctly, and many experienced people get it wrong.
In order to correctly handle input errors and limit it so that only lower case x will break your loop, you need to do a lot of error checking:
#include <iostream>
#include <sstream>
#include <string>
int main()
{
int neg = 0;
int pos = 0;
int zer = 0;
std::string line;
while (std::cin >> line)
{
if (line == "x")
{
break;
}
std::istringstream iss(line); // convert to a stringstream
int val = 0;
if (!(iss >> val)) // if we can load an int, do it, otherwise show and error message
{
std::cout << "Please enter a valid number!" << std::endl;
continue;
}
if (val > 0)
{
pos++;
}
else if (val < 0)
{
neg++;
}
else
{
zer++;
}
}
std::cout << "You entered " << pos << " positive numbers.\n"
<< "You entered " << neg << " negative numbers.\n"
<< "You entered " << zer << " zeros." << std::endl;
return 0;
}
The problem is that an object of type int may not read symbols as for exmaple 'x' from a stream. It expects digits in an input stream. So when a symbol that can not be in a number is encountered in an input stream an error is arised. The stream will have erroneous state. If you will try again and again to read a number the stream will give nothing due to its state and the fact that the next symbol is for example non-digit.
So there is no sense to compare variable input with 'x'.
I would rewrite your loop the following way
while ( true )
{
int input;
cout << "Input another positive/negative number or 'x' to stop: ";
if ( !( cin >> input ) ) break;
if (input > 0)
{
pos++;
} else if (input == 0)
{
zer++;
} else
{
neg++;
}
}
check this out
#include <iostream>
#include<string>
using std::string;
using std::getline;
using namespace std;
int main()
{
string input;
int neg = 0, pos = 0, zer = 0;
char z;
input = "";
int iVal = 0;
do
{
cout << "Input another positive/negative number or 'x' to stop\n";
getline(cin, input);
iVal = atoi(input.c_str());
if (input != "x" && input !="X")
{
if (iVal > 0)
{
pos++;
} else if (iVal == 0)
{
zer++;
} else if(iVal < 0)
{
neg++;
}
}
} while (input != "x" && input != "X");
cout << "You entered " << pos << " positive numbers.\n";
cout << "You entered " << neg << " negative numbers.\n";
cout << "You entered " << zer << " zeros.\n";
return 0;
system("pause");
}
IT goes into the loop for a few reasons
1)You declared input as an integer, for this purpose ypu would have to declare it as char data type in order to do your validation on it
2)You dont have a if condition for x eg else if (input =='x')