So I am very new to coding, "hello world" was three weeks ago, but the professor gave us the assignment to track different information about the numbers inputted. The code tracks the average value, max, min, sum, and number of values inputted.
The prof also had us put an if loop in it where if the user inputted a -1 the code would break. I would like to be able to input -1 so I am trying to set change the way the code breaks.
The way I thought to do this was to prompt for a character, and if the character was q break the code, but if it wasn't to convert it to an integer. I read that you can use the atoi function to do this but I can't seem to get it to work. I'm sure it's something trivial that I just don't understand, so sorry for the dumb question. Thanks for the help!
#include<iostream>
#include <stdio.h>
#include <stdlib.h>
#include <cstdlib>
usingnamespace std;
int main()
{
int userInput = 0;
char quit;
int count = 0;
int total = 0;
int big;
int tiny;
while(1) {
cout << "Input numbers (type q to stop)" << endl;
cin >> quit;
if(quit == 'q'){
break;
}
else{
userInput = atoi(quit);
if(count == 0){
big = userInput;
tiny = userInput;
}
if(userInput > big){
big = userInput;
}
if(userInput < tiny){
tiny = userInput;
}
total = total + userInput;
count++;
}
}
float average = total/count;
cout << "__________________________________" << endl;
cout << " " << endl;
cout << "So, you entered a total of " << count << " numbers." << endl;
cout << "Sum of all those numbers: " << total << endl;
cout << "The average value is: " << average << endl;
cout << "The smallest number you entered was " << tiny << "." << endl;
cout << "The largest number you entered was " << big << "." << endl;
cin.get();
cin.get();
return 1;
}
cin >> input; - this gets the user to input something via the keyboard, and store it as a string. It's possible that the user might just press enter, in which case the string would be empty.
Hence this test, if (input.length() > 0), if the string is empty, we can't do anything useful with it. An alternative would have been the .empty() function.
This line if (input[0] == 'q') is accessing an individual character in the string. [0] is the first character, [1] is the second and so on.
Finally this line int number = atoi(input.c_str()); uses the atoi() function which gets an integer from a string of characters. It needs a plain c-string, not a C++ string. That's the purpose of the .c_str() function.
vector<int> nums;
char ch;
while (true)
{
cin >> number;
if (cin.fail())
{
cin.clear(); // Not a number. Clear the error condition
cin >> ch; // and get a character
if (ch == 'q')
{
break; // 'q' will terminate the 'while' loop
}
else
{
cout << " invalid input ignored\n"; // it wasn't 'q', so continue
cin.ignore(1000,'\n'); // ignore the incorrect input
}
}
else
{
nums.push_back(number); // store the valid number
}
}
// Do something with the numbers which were previously input.
cout << "\n -- Processing -- \n";
for (int i=0; i<nums.size(); ++i)
{
cout << nums[i] << endl;
}
cout << "\n --- Finished --- ";