Write a program that reads in a sequence of integers provided by the user, and then, when the user enters a negative integer, displays the sum of all the previous positive ones. (S) YOU MUST NOT use an array to save the all the entered numbers – you do not need to.
I can figure it out if I had to use an array but without using an array cant even think.
I have wrote the program and it can take the numbers till the user enters a negative number but then I don't know what to do.
Any suggestions are welcome just to point me in the right directions.
You need 2 variables: 1 that holds the total addition (starts with 0) and the 2. is the entered number. Then you need an if that checks whether the entered number is positive (-> add to total) or negative (-> break the loop)
Here is the code although u might have problems as I'm using Gwin its just a stupid C++ complier although I understand normal c++ so just help me the normal C++.
this is what it bascially means when you write from Gwin to normal C++
cin>>x;
cout<<x;
by (assuming x is an int)
x= Gwin.readInt();
Gwin.writeInt(x);
int main()
{
int number = 0;
int n = 0;
int numbersum = 0;
int a,b,c,d,e,f,g,h;
GWindow Gwin;
// Clear the Gwin window
Gwin.clear();
Gwin.setPenColour(BLACK);
while (number>=0)
{
Gwin.clear(); // Clears window for new entry
Gwin.setPenColour(BLACK); // set pen colour to black i.e. font = black
Gwin.writeText(20, 20, "Enter sets of numbers: "); // writes on the screen asking user to enter number
number = Gwin.readInt(); // Reads the number enetered and saves it in int number
}
Gwin.writeText(20, 40, "Sum = "); // writes on the screen
Gwin.writeDouble (90,40,number); // displays the value in int number
// Finally, wait for a key to be pressed
Keyboard.waitKey();
return 0;
}
I know the code above will never get me the right answer because its just not adding anything but i just dont know how to get the previous entered number to add
cin>>x;
cout<<x;
by (assuming x is an int)
x= Gwin.readInt();
Gwin.writeInt(x);
int main()
{
int number = 0;
int n = 0;
int numbersum = 0;
int a,b,c,d,e,f,g,h;
//added by gaorozcoo
long accumulator=0;
//////////////////////
GWindow Gwin;
// Clear the Gwin window
Gwin.clear();
Gwin.setPenColour(BLACK);
for(;;) //while (number>=0)
{
Gwin.clear(); // Clears window for new entry
Gwin.setPenColour(BLACK); // set pen colour to black i.e. font = black
Gwin.writeText(20, 20, "Enter sets of numbers: "); // writes on the screen asking user to enter number
number = Gwin.readInt(); // Reads the number enetered and saves it in int number
//added by gaorozcoo
if(number>=0){
accumulator+=number;
}else{
break;
}
/////////////////////////
}
//Now you can print the variable accumulator.
Gwin.writeText(20, 40, "Sum = "); // writes on the screen
Gwin.writeDouble (90,40,number); // displays the value in int number
// Finally, wait for a key to be pressed
Keyboard.waitKey();
return 0;
}