# help???

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 09-15-2004
dAzed
help???
I'm in my second week of programming and need some help here is what i have so far

#include <iostream> /*for cin and cout*/

int main () /*function main*/
{
int x,x2,x3,sum,number=0,product=0,largest=0,smallest= 0; /*declaration of variables*/
float avg;

std::cout << "Please enter an interger" << std::endl; /*input data*/
std::cin >> x;
std::cin >> x2;
std::cin >> x3;

sum = x + x2 +x3; /*total score*/
avg = (sum)/2; /*average score*/

std::cout << "The number of the integers is" << number << std::endl;
std::cout << "The sum of the integers is" << sum << std::endl;
std::cout << "The average of the integers is " << avg << std::endl;
std::cout << "The product of the integers is " << product << std::endl;
std::cout << "The largest integers is " << largest << std::endl;
std::cout << "The smallest integers is " << smallest << std::endl;
return 0;
}

is there any way to have infintive input until a user enters -1???
• 09-15-2004
Vicious

Then search the board a little and look for while loops.

Basically

Code:

``` do {   // stuff you want } while ( input != -1 );```
• 09-15-2004
sure, it's called control loops.
Code:

```int dummy = 0;  //this is called a flag because of how it's used   while (dummy != -1) //here's the start of the loop {   std::cout << "Please enter an interger" <<        std::endl; /*input data*/   std::cin >> x;   std::cin >> x2;   std::cin >> x3;   sum = x + x2 +x3; /*total score*/   avg = (sum)/2; /*average score*/   std::cout << "The number of the integers is" << number << std::endl;   std::cout << "The sum of the integers is" << sum << std::endl;   std::cout << "The average of the integers is " << avg << std::endl;   std::cout << "The product of the integers is " << product << std::endl;   std::cout << "The largest integers is " << largest << std::endl;   std::cout << "The smallest integers is " << smallest << std::endl;     //here's where user decides to stop or continue   std::cout << "enter -1 to stop, anything to do it all over again" << std::endl;   std::cin >> dummy; }```
• 09-15-2004
dAzed
thanks for the welcome i had kinda tried that but was wondering does all input from the user have to be assigned to something like x, x1, x2, x3 or could a user enter intergers endessly without having them assigned to x,x1,x2,x3 does that make any sense?
• 09-15-2004
Vicious
Well the input needs to go into a variable. So just create a flag like elad stated.

Code:

```int flag; do {     cout << "Show this again? ( -1 for no ): ";     cin  >> flag; } while ( flag != -1 );```
You can endlessly write to flag.
• 09-15-2004
dAzed
thanks for the help ill be bcak im sure
• 09-15-2004
std::cout << "Please enter an interger" << std::endl; /*input data*/
std::cin >> x;
std::cin >> x2;
std::cin >> x3;

with this snippet all user input up to the first time the user enters a white space character (space, tab, enter key, etc.) will go into x if the input is valid. The program will then pause until additional data and another whitespace char is entered. That data, if valid, will be put into x2. Then the program will pause again until additional data is entered and a third whitespace char is entered. That data, if valid, will go into x3. Once x, x2, and x3 have valid data, then the program will continue on. While it is possible to do this all at once as below:
Code:

```s is a space key in the following and E is the Enter key   1s23s456E   now x contains 1, x2 contains 23, and x3 contains 456. There has been only one enter key pushed.```
it is not usually done like this because the user has no of knowing that x2 and x3 exist. The programmer does, but the end user doesn't, unless they are told:

std::cout << "enter first integer" << std::endl;
std::cin >> x;
std::cout << "enter second integer" << std::endl;
std::cin >> x2;
std::cout << "enter third integer" << std::endl;
std::cin >> x3;

now the enter key has been used three times, but it is absolutely clear what your intentions were.

If you want to have user enter three integers separated by a space or some other whitespace char, tell them:

std::cout << "enter three integers separated by a space" << std::endl;
std::cin >> x;
std::cin >> x2;
std::cin >> x3;
• 09-15-2004
DougDbug
Quote:

could a user enter intergers endessly without having them assigned to x,x1,x2,x3 does that make any sense?
You can use an array. Your variabwle will look like this: x[0], x[1], x[2]... You can set your own limit by the size of array you make. You'll need to count the number of numbers input.
• 09-15-2004
Lithorien
elad: Why don't you use "bool" for your flag rather than "int"? It seems like it would save a little memory, since bool can only hold 1 or 0.

Just wondering.
• 09-15-2004
Sebastiani
>> elad: Why don't you use "bool" for your flag rather than "int"?

>> is there any way to have infintive input until a user enters -1???

>> It seems like it would save a little memory, since bool can only hold 1 or 0.

the smallest addressable unit of data is the byte, and that's why a bool can't be implemented with anything less than a byte (otherwise, you couldn't instantiate a bool*).
• 09-15-2004
Lithorien
Thanks for the clarification, Sebastiani. :)

As to the OP, you can always call a menu() function from within main().. something like:

Code:

```int main() {     bool flag = 0;     while (flag != 1)     {         menu();     }     return(0); } int menu() {     // do stuff...     if (// user says yes to some kind of "are you done" question)     {         flag = 1;         return(0);     }     return(0); }```
• 09-15-2004
Sang-drax
My programming teacher would've killed me if I'd handed in code like this:
Code:

`while (dummy != -1)`
Point is: use meaningful variable names.
• 09-15-2004
Vicious
Oh and if you use bool, you might as well check.assign true and false instead of 1 and 0.
• 09-15-2004
jlou
Quote:

Originally Posted by Lithorien
As to the OP, you can always call a menu() function from within main().. something like:

Ummm, that won't work, flag is local to main, and won't get updated even if it compiled (it wouldn't because flag is unidentified in menu()). Maybe something more like:
Code:

```int main() {     bool flag = false;     while (!flag)     {         flag = menu();     }     return(0); } bool menu() {     // do stuff...     if (// user says yes to some kind of "are you done" question)     {         return true;     }     return false; }```
• 09-15-2004
Hunter2
>>could a user enter intergers endessly without having them assigned to x,x1,x2,x3 does that make any sense?

As DougDbug said, you can start off using an array. Then when you're confident, you can try out using dynamic arrays. And when you're pretty good with those, look up std::vector or std::list, or even just "STL containers". Very useful, those things :) But takes a while to work up to the level where you're ready to use them.
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last