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.
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;
}
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.
#include "stdafx.h"
#include <iostream>
usingnamespace 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;
}
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