Confused and cant get my head around this

Hey, If anyone can help me with this.

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.




Thanks

Please post the program you have written so we can help you. Please remember to use code tags.
Last edited on
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
Try this and let me know

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
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;
}
Last edited on
sorry no it don't work it just shows the last value like it use to do it previously
Line 50 must be: Gwin.writeDouble (90,40,accumulator); // displays the accumulator
Sorry, please change line 50 of the code I sent you

change
 
Gwin.writeDouble (90,40,number);

to
 
Gwin.writeDouble (90,40,accumulator);

sorry internet was down for 2 days but it works THANKS!! was doing my headin and learnt something new!
Topic archived. No new replies allowed.