unable to enter n in the below code( cin>>n) - c++

/**
Write a program that reads a series of numbers and stores them in a vector. After the user inputs all the numbers he or she wishes to, ask how many of the numbers the user wants to sum. For an answer N. print the sum of the first N elements of the vector. For example: "Please enter some numbers (press 'I' at prompt to stop ) : " 12 23 13 24 15 "Please enter how many of the numbers you wish to sum, starting from the first:" 3 "The sum of the first 3 numbers : 12, 23, and 13 is 48." Handle all inputs. For exam ple, make sure to give an error message if the user asks for a sum of more numbers than there are in the vector.
**/
#include<iostream>
#include<vector>
using namespace std;
int main()
{
try
{
vector<int> numbers;
int num;
cout<<"Now enter the numbers";
while(cin>>num)
numbers.push_back(num);
int n,sum=0;
cout << "Enter the nth number to find sum of elements till n : ";
cin>>n;
if(n >numbers.size())
throw 66;
for(int i=0;i<n;i++)
sum+=numbers[i];
cout << "sum is "<<sum;
return 0;
}
catch(int k)
{
cerr<<"Error "<<k;
return -1;
}
}
So , when I enter EOF , CTRL+D , the program terminates. I am not sure where it is going wrong. I even tried to debug using gdb(with the help from an online tutorial) . It didn't just work out . can someone tell me what's wrong with the code ?

You are not checking if you actually read anything.
Consider this little test program:
#include <iostream>
int main()
{
std::cout << "std::cin is " << (std::cin ? "ready" : "done") << "\n";
int n = -42;
std::cin >> n;
std::cout << n << "\n";
std::cout << "std::cin is " << (std::cin ? "ready" : "done") << "\n";
n = -42;
std::cin >> n;
std::cout << n << "\n";
std::cout << "std::cin is " << (std::cin ? "ready" : "done") << "\n";
}
The output, when fed with an empty standard input (which is equivalent to immediately declaring its end with ctrl+d) is:
std::cin is ready
-42
std::cin is done
-42
std::cin is done
As you can see, n is never changed, as there is never a new value to change it to! Also, you can easily spot that the state of std::cin reflects if the previous read went past the end.
Since you are only checking the value of your integers without ensuring that they have a sane default (just check what happens to n if it is not set by reading the input), this can easily lead to your program exhibiting unexpected behavior.
Note: The behavior of the test program is different when fed input that simply is not a number.

When you supply cin with EOF it causes cin.failbit to become true. With the failbit set to true, all subsequent cin reads will be ignored. Since n has no default value execution becomes unpredictable from here. In my case the program was crashing because it was throwing 66. Adding cin.clear() after the while loop will fix this, but is not advisable. Two simple solutions would to stop on a magic number/prompt the user after every input if they want to continue.

Related

Hi I want user to enter 5 numbers and then output the sum in the end

I have used a loop for that:
int number1;
int sum=0;
for(int i =1; i<6; i++){
cout<<"Enter number:\n";
cin>>number1;
sum+=number1;
}
cout<<sum;
cout<<"Total Sum is = "<<sum<<"\n";
return 0;
}
My question is how can I print first statement like this ...
"Enter first number"
Enter Second number" and so on
Whenever you are reading numbers (or any value for that matter), you must check the stream-state (see: std::basic_istream State Functions). You have four stream states you must test following every input:
.bad() or .eof(). If badbit is set an unrecoverable error occurred, and if eofbit is set, there is nothing more to read (you can combine both into a single test that exits if either are set)
.fail() is set when a read error occurs, such as the user entering "FIVE" instead of 5 where integer input is expected. You handle failbit being set by calling .clear() to clear failbit and then call ignore() to empty the characters causing the failure before your next read attempt, and finally
.good() - valid input was received from the user, you can proceed to the next input.
By validating your input here, you can Require the user provide 5 valid integer values for you to sum. Do not use a for loop, instead use a while (or do .. while();) and only increment your counter when good input is received.
Putting that altogether, you can do:
#include <iostream>
#include <limits>
int main (void) {
int number = 0,
sum = 0;
const char *label[] = { "first", "second", "third", "fourth", "fifth" };
while (number < 5) /* loop continually until 5 int entered */
{
int tmp; /* temporary int to fill with user-input */
std::cout << "\nenter " << label[number] << " number: ";
if (! (std::cin >> tmp) ) { /* check stream state */
/* if eof() or bad() exit */
if (std::cin.eof() || std::cin.bad()) {
std::cerr << " (user canceled or unreconverable error)\n";
return 1;
}
else if (std::cin.fail()) { /* if failbit */
std::cerr << " error: invalid input.\n";
std::cin.clear(); /* clear failbit */
/* extract any characters that remain unread */
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
}
else { /* on succesful read of int, add to sum, increment number */
sum += tmp;
number++;
}
}
std::cout << "\nsum: " << sum << '\n';
}
Now your code will gracefully handle an invalid input without exiting just because a stray character was entered.
Example Use/Output
When you write an input routine, go try and break it. Enter invalid data and make sure you handle all error cases correctly. If something doesn't work right, go fix it. Repeat until you input routine can handle all corner-cases as well as the cat stepping on the keyboard:
$ ./bin/sumintlabel
enter first number: 3
enter second number: four five six seven!!
error: invalid input.
enter second number: 4
enter third number: 5
enter fourth number: 6
enter fifth number: 7
sum: 25
Form good habits now regarding handling input, it will pay dividends for the rest of your programming career. Let me know if you have questions.
if you need to print the words "first"... untill "fifth" then I'd do it like this:
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
int number1;
int sum=0;
string positions[5] = {"first", "second", "third", "fourth", "fifth"};
for(int i = 0; i<5; i++){
cout<<"Enter the " << positions[i] << " number:" << endl;
cin>>number1;
sum+=number1;
}
cout<<"Total Sum is = "<<sum<<"\n";
return 0;
}
I used an array of strings to display the words and changed the for loop to start at 0 so that we can go through the array positions and add the 5 numbers as well. If you just want to use: 1st, 2nd, 3rd... then you could change the for loop to what it was and do:
cout<<"Enter the " << i << "st" << " number:" << endl;
But for this you would have to use the if statement to print the right endings("st", "rd", "nd"). I think it would take longer for it to run but its miliseconds so we wouldn't even notice hahaha.
Hope it helped :)
You can use switch():
#include <iostream>
using std::cin;
using std::cout;
using std::string;
int main() {
int number1;
int sum = 0;
for(int i = 1; i < 6; i++) {
string num;
switch(i) {
case 1:
num = "first";
break;
case 2:
num = "second";
break;
//and 3 4 5 like this
}
cout << "Enter " << num << " number:\n";
cin >> number1;
sum += number1;
}
cout << "Total Sum is = " << sum << "\n";
return 0;
}
or you can use struct or containers like vector (in fact you have to use containers if you want to get a huge number of data.)

Need some help finding a logical error in my while loop

I am learning C++, and I am doing some exercises in the book I am using. One of them asks to write a program that asks a user how many numbers they want to add up. Then prompt for the numbers the user wants to add or to enter '|' once finished. The numbers are then pushed into a vector. Part of the program asks to check if the size of the vector is equal to the original number of input items and that is where I keep getting an error.
cout << "Please enter the numbers and | once you are done: ";
while(true)
{
for(int num; cin >> num; )
{
if(num == '|')
{
break;
}
ints.push_back(num);
}
if(ints.size() != n)
{
cout << "There are more or less numbers in the vector than originally specified\n"
<< "Vector will be cleared; please re-enter the values: ";
ints.clear();
continue;
}
else
{
break;
}
}
The problem is that if the number of input is off, the message goes into an infinite loop and I am not sure how to fix it.
EDIT: n is the variable that holds in the number of values user wanted to enter.
Thanks!
num is an integer and cin >> num won't extract | symbol. Comparison num == '|' may not work as expected because num could have the numeric value of | ascii symbol even when user did not input any | symbol. You should properly handle end marker reading:
// loop will break when user enters `|` because it is not an integer
// setting failbit of cin
for(int num; cin >> num;)
{
ints.push_back(num);
}
cin.clear(); // reset failbit making cin able to read again
// check the end marker entered by user
{
string end_marker;
cin >> end_marker;
if("|" != end_marker)
{
// use of endl will flush the stream ensuring that
// printed text won't stuck in the buffer
cout << "Please use | as end marker" << endl;
continue;
}
}
Here is how I implemented it. I am worried about the logic in your while loop. I had been taught to avoid while(true) whenever possible. You know the logic behind how your code should work. With more practice you'll start to recognize the conditions you need to use. I am sure there are better ways to do it. But this is the way I tried it.
But to answer your question, the main reason it is failing is because integers cannot compare themselves with characters.
if(num == '|')
That does not work since num is an integer and not a character.
Normally I would implement this in a class and since global variables are not highly looked upon I created my own namespace. You'll have to finish the rest of the logic yourself however:
#include <iostream>
#include <vector>
#include <string>
namespace global
{
std::vector<std::string> strings;
std::vector<int> ints;
std::string a = " ";
int num = 0;
}
void doWork()
{
std::cout << "Please enter the number of integers you would like to add up: ";
std::cin >> global::num;
std::cout << "Please enter the numbers and | once you are done: ";
while (global::a != "|")
{
std::cin >> global::a;
global::strings.push_back(global::a);
}
global::strings.pop_back();
for(auto &e : global::strings)
{
global::ints.push_back(std::stoi(e));
}
}
int main()
{
doWork();
if(global::ints.size() != global::num)
{
std::cout << "Size of vector does not match the size specified. Clearing vector" << std::endl;
global::ints.clear();
global::strings.clear();
global::num = 0;
global::a = " ";
doWork();
}
}
I made a vector of char's and converted those into integers so that way you could add them up. The while loop should be checking for | rather than always running true. It then will check the size of the vector in the end, clear it if it does not match, and ask you to do it again. This is the best way that I could think of doing it.
EDIT: as VTT pointed out, char can only do one character at a time. I have converted it into a string in order to handle the conversion.
EDIT 2: reset the values of global::num and global::a to their default at the end of the failure in order to prevent crashing.

Step program? Start, end, step integers

I've been stuck on this for almost two weeks now, could someone guide me? I'm supposed to have the user input an initial start value, and integer they want to end with, and a stepping integer (what the start value will be multiplied by).
EX.
Starting Integer: 10
Ending Integer: 200
Stepping Integer: 20
Output: 10, 30, 50, 70, 90, ...., 200
And when user inputs false values like a smaller ending integer than starting integer, message prompts them to try again.
I got the whole message prompting down I believe, not sure how to get the starting integer to be multiplied by stepping, and stop at ending integer.
This is the code I have so far:
#include <iostream>
#include <string>
using namespace std;
int main()
{
int step;
int stop;
int start = -1; //Local variable declaration
cout << "Please enter a positive integer you'd like to start with." << endl; //Starting integer
cin >> start;
if (start < 0)
cout << "Please enter a positive integer.";
if (start >= 0)
cout << "Please enter an integer to end with, it must be bigger than previous number chosen."; //Ending integer
cin >> stop;
if (stop < start)
cout << "Please enter a value that is larger than the previous number chosen." << endl;
if (stop > start)
cout << "Please enter the value you'd like to increase by." << endl; //Stepping integer
cin >> step;
cout << "Integers:";
while (start < stop)
{
cout << start << ", ";
start+=step;
}
return 0;
}
When I run it, it'll go through the entire for loop, and display the while loop but as only the stepping integer the user input, and then exits immediately.
EDIT - Code above is updated from previous code
Thanks for all the help so far! I've taken out the for loop as mentioned, and removed 'cin >> start;' after "Please enter a positive integer." - That was the reason why my program wasn't running through the whole cycle.
The program runs flawlessly if the user follows instructions first time around, however if they input a negative value first, they'll get the results below:
Input: -5
Output: Please put in a positive integer.
Input: 5
Output: Please enter the value you'd like to increase by.
And if they enter a stop value smaller than start value, they'll get the results below:
Input: 5
Output: Please enter a stop value larger....
Input: 3
Output: Integers:.........
How would I get the prompts to continue in order even after the user inputs negative value at first, and/or a smaller stop value than start? I don't think it'd be a cin; command, but please correct me if I'm wrong!
Maybe this can help:
int start = 10;
int stop = 200;
int step = 20;
do
{
cout << start << " ";
start = start + step; // increment. Shorter version: start += step;
} while (start < stop);
When getting the input try something like
int start = -1;
while(start < 0)
{
cout << "Please enter a positive integer you'd like to start with." << endl;
cin >> start;
}
and do something similar for stopand step, for instance
int end = -1;
while(end < start)
{
cout << "Please enter an integer to end with, it must be bigger than previous number chosen."; //Ending integer
cin >> end;
}
Instead of incrementing start by 1, increment it by step.
Also, you need to output the start variable, not step. Outputting step as you did before will just result in:
Integer: 20
Integer: 20
Integer: 20
....
Here are the changes I made:
cout << "Integers: ";
while(start < end)
{
cout << start << ", ";
start+=step;
}
Also, the for loop is not needed, it breaks out of it at the bottom regardless, so just delete the for loop.

Why does a string become an integer in C++?

#include <iostream>
using namespace std;
int main () {
int N;
cout << " Input an integer:";
cin >> N;
cout << " The integer entered is:" << N << endl;
}
when I input an Integer it returns the same value but when I input hello it gives me 1961729588.
The string doesn't become an integer, the std::cin operation fails and what you get as output is the garbage value that was in N originally. Initialize N to 0, and type in "hello" you should see 0 as output.
"when I input an Integer it returns the same value but when I input hello it gives me 1961729588?."
The cin >> N; actually fails returning false for the stream state, when a input is given that cannot be converted to an integer. You can check for such error condition with
if(!(cin >> N)) {
cerr << "Input a valid number!" << endl;
}
else {
cout << " The integer entered is:" << N << endl;
}
The value of N will be initialized (reset) to int() (default value) which actually renders to 0.
Full live sample
#include <iostream>
using namespace std;
int main () {
int N;
cout << " Input an integer:";
if(!(cin >> N)) {
cout << "Input a valid number!" << endl;
cout << "N = " << N << endl;
}
else {
cout << " The integer entered is:" << N << endl;
}
return 0;
}
Input
Hello
Output
Input an integer:Input a valid number!
N = 0
This was cross checked with a Ideone code sample
I cannot reproduce getting some garbage value like 1961729588. The value was correctly reset by the std::istream& operator>>(std::istream&, int&); input operator.
Is it an issue of your current compiler's implementation, c++ standards level (-std=c++11) settings?
I have found some notes about eventual differences regarding c++ standards at cppreference.com:
Though I didn't spot what they really refer to with 'a value as described above', to be honest.
When you input a non-integer the input fails. When the input fails, N retains its original value which isn't defined, i.e., writing results in undefined behavior. You should test your inputs, e.g.:
if (std::cin >> N) {
// do something with the successful input
}
else {
// deal with the input failure
}
When you enter cin >> N; the compiler sees that N was declared as an int. Thus your program will call a function that will attempt to read text representing an int from cin and store the result in N.
To do this it will read as much numeric characters from cin as it can, and stop when a non-numeric character is encountered.
For example if you enter 32\n your program reads 3, then 2, then \n. When it sees the \n it stops reading, because \n is not a number. The program will push \n back on to the stream (in case we want to read it later) and store 32 in N.
Suppose instead of a number you type some word such as "hello". The your program will read h and then stop, because h is not a number. h will be pushed back onto the stream (to be read later) and nothing will be stored in N. cin will return an error since no numeric characters were read.
This still does not explain the value of 1961729588.
Notice N was never initialised. According to the C++ Standard the value of an uninitialised automatic variable is undefined. Thus the value of N will be some garbage value. In your case this was 1961729588.

do while loops can't have two cin statements?

I'm just following a simple c++ tutorial on do/while loops and i seem to have copied exactly what was written in the tutorial but i'm not yielding the same results. This is my code:
int main()
{
int c=0;
int i=0;
int str;
do
{
cout << "Enter a num: \n";
cin >> i;
c = c + i;
cout << "Do you wan't to enter another num? y/n: \n";
cin >> str;
} while (c < 15);
cout << "The sum of the numbers are: " << c << endl;
system("pause");
return (0);
}
Right now, after 1 iteration, the loop just runs without asking for my inputs again and only calculating the sum with my first initial input for i.
However if i remove the second pair of cout/cin statements, the program works fine..
can someone spot my error please? thank you!
After you read the string with your cin >> str;, there's still a new-line sitting in the input buffer. When you execute cin >> i; in the next iteration, it reads the newline as if you just pressed enter without entering a number, so it doesn't wait for you to enter anything.
The usual cure is to put something like cin.ignore(100, '\n'); after you read the string. The 100 is more or less arbitrary -- it just limits the number of characters it'll skip.
If you change
int str;
to
char str;
Your loop works as you seem to intend (tested in Visual Studio 2010).
Although, you should also probably check for str == 'n', since they told you that they were done.
...and only calculating the sum with my first initial input for i...
This is an expected behavior, because you are just reading the str and not using it. If you enter i >= 15 then loop must break, otherwise continues.
I think you wanted this thing
In this case total sum c will be less than 15 and continue to sum if user inputs y.
#include<iostream>
using namespace std;
int main()
{
int c=0;
int i=0;
char str;
do
{
cout << "Enter a num: \n";
cin >> i;
c = c + i;
cout << "Do you wan't to enter another num? y/n: \n";
cin >> str;
} while (c < 15 && str=='y');
cout << "The sum of the numbers are: " << c << endl;
return 0;
}