Easy code returns crazy numbers, only allows me to enter 1 variable. I will continue to work on this and it's probably easy, but for now bed soon.
Just tinkering and learning in my free time for fun. Was wondering about buffers and flushing them ... am I getting something stuck in the buffer to return a crazy number?
Also how to
std::endl; a std::cin operation???? let me know
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
#include <iostream>
int main()
{ // page 19 has an example of a program like this and how it works.
int x;
int y;
std::cout << " Enter two numbers to define Y and X variables:" << std::endl; // cout string end line
std::cout << " Enter a value for X" << std::endl; // again string end line
std::cin >> x; // enter amount for x
std::cout << " Enter a value for Y" << std::endl; // string end line
// note example in the double forward slash. This is a basic input example program for begginers.
/* this is another note exaple */
std::cout << "The sum of entered variables X and Y is " << x + y; // cout sum of variables
return 0;
}
You are missing a std::cin statement after you ask the user to enter a value for Y. This leaves some junk value in y, which causes the crazy numbers you see.
Also, you cannot use std::endl with istream. std::endl is only for streaming a newline character to ostream (or std::cout) and flushing the ostream buffer. If you really need another newline after the user input, add "\n" before "Enter a value for Y" and so forth.
Yeah .. you can now tell 100% this was so easy. My solution to this problem was sleep all along haha....
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#include <iostream>
int main()
{ // page 19 has an example of a program like this and how it works.
int x;
int y;
std::cout << " Enter two numbers to define Y and X variables:" << std::endl; // cout string end line
std::cout << " Enter a value for X" << std::endl; // again string end line
std::cin >> x; // enter amount for x
std::cout << " Enter a value for Y" << std::endl; // string end line
std::cin >> y;
// note example in the double forward slash. This is a basic input example program for begginers.
/* this is another note exaple */
std::cout << "The sum of entered variables X and Y is " << x + y; // cout sum of variables
return 0;
}