need help with hw

can someone expalain me this? i have no idea what i have to do..

• Write a program that asks the user to input an integer and then read the integer,

• Modify the program so that your program reads three integers one at a time. Each time the program asks one integer to enter and then reads the integer. Use while loop to do this task.

• Again modify your program which will also add all the integers and then display the final sum.
What is the first confusing part to you? For me, I cannot understand why they would have you use a while loop instead of a for loop.
closed account (3qX21hU5)
Probably because they usually teach while loops before for loops since they can be easier to understand when just beginning. Atleast I think?

Anyways

Like LB said not sure where you are stuck at here is a framework to get you started though.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
int main()
{
    // Declare your int variable here


    while (// Put something here to make it stop after 3 times)
    {
        // This is where you have the user input a integer and then read the
        // the integer back to them.

        // For step 3 you will need to add all three integers together (hint use
        // another variable to hold them)
    }

    // For step 3 display the final sum

    return 0;
}


Last edited on
What do i put after while() to make it stop after 3 times?

Thanks for the help btw, i really appreciate it
You need the loop condition. Recall how while loops work:
http://www.cplusplus.com/doc/tutorial/control/#while

You will need a variable to track the number of times the loop has run.
/Help please lol..i have no idea what im doing..


/ Whileloop.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
using namespace std;

int main()
{
int var1=1;
int var2=2;
int var3=3;


while (var1,var2,var3>0)
{

cout << "Enter interger 1:" << endl;
cin >> var1;
cout << "Enter interger 2':" << endl;
cin >> var2;
cout << "Enter interger 3:" << endl;
cin >> var3;
cout << "Your numbers are:" << endl;
cout << var1;
cout <<",";
cout << var2;
cout <<",";
cout << var3;

// For step 3 you will need to add all three integers together (hint use
// another variable to hold them)
}

system("Pause");

// For step 3 display the final sum

return 0;
}
Last edited on
you have it where it goes through all three....three different times....for loop would make more sense but here is how the loop should look

1
2
3
4
5
6
7
8
9
10
11
12
int var[3];

int n=0;
while(n!=2)
{
cout<<"Enter variable"<<n;
cin>>var[n]<<endl;
n++;
}
cout<<var[0]<<var[1]<<var[2]<<endl;
cout<<"Sum is:"<<var[0]+var[1]+var[2];
Last edited on
Can you use an array?
closed account (3qX21hU5)
There is no need for a array it would just complicate things. Also there is no need for 3 different variables. All the homework assignment says to do is have the user input 3 numbers and print each number as it is entered. So you can use one variable for that. For the third atep you need to create a new variable and just add each number the user enteres to it.
Ok this is the latest i did..the program complies but it has some weird results coming up
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include "stdafx.h"
#include <iostream>
using namespace std;

int main()
{
    int var[3];
	int n=0;
	while (n!=2)
	{
		cout<<"Enter a variable:";
	cin>>var[n];
	n++;
	
	}
	cout<<var[0]<<var[1]<<var[2]<<endl;
    cout<<"Sum is:"<<var[0]+var[1]+var[2];

	system("Pause");

    // For step 3 display the final sum

    return 0;
}
I'm sorry i told you wrong where you have
 
	while (n!=2)

change the number to 3....im sorry

also might wanna put spaces where it displays the numbers

There is no need for a array it would just complicate things. Also there is no need for 3 different variables. All the homework assignment says to do is have the user input 3 numbers and print each number as it is entered. So you can use one variable for that. For the third atep you need to create a new variable and just add each number the user enteres to it.

There are many ways to skin a cat....Tbh your way does sound simplier but when i thought about the problem this is just what came to my mind
Last edited on
Topic archived. No new replies allowed.