For loop and while Problem


Thank you for looking at this.
SO my problem now is that the for loop won't run for the
amount given. it also wont do the find the sloution to the
the problem total = total + num1.

On the second half I can't get the while to quit with input.
also it will not at one to Even_Num?

Please help me. thank you




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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
 *Author         :   
 *Date           :   
 *Course/Section :   CSC110-???
 *Program Description: This lab introduce repitions statments.  Your
 *   Program will as the user for how many mubers to input.  You will
 *   then code a FOR loop to add up the numbers.  Display the sum and
 *   pause the screen. THEN your program will input an unknown number
 *   of positive integers.  Count and display the number of even 
 *   numbers.  Enter a -1 to stop entering in numbers
 *
 *BEGIN Lab 04 - Repitions
 *   Init total to 0
 *   Input count of how many numbers to input
 *   FOR (Each number to input)
 *      Enter the number
 *      Add the number to the total
 *   END FOR
 *   Clear the screen
 *   Display the total
 *   Pause the screen
 *   Clear the screen
 *   Init count of even numbers to 0
 *   Input a positive number (or QUIT)
 *   WHILE (input number is not QUIT)
 *       IF (Input Number is even)
 *           Add 1 to count of even numbers
 *       END IF
 *       Get next input number or quit
 *   END WHILE
 *   Display the count of even numbers
 *END Lab 04 - Repetition
 ********************************************************************/

#include <iostream>
#include <iomanip>
using namespace std;

void main()
{
	//local constants

	const int QUIT = -1;
	//local variables
	int Number;
	int Num1;
	int Num2;
	int Total = 0;
	int Even_Num;
	int Count;
	
	/**************************start main program*********************/


	//Prompt for number input
	cout << "\n\n\n\n\n\n\n";
	cout << setw(50)  << "------------------------------"  << endl;
	cout << setw(50)  << "  Enter an amount of numbers  "  << endl;
	cout << setw(50)  << "------------------------------"  << endl;
	cout << "\n" << setw(46)  <<  "Amount:  ";
	cin >> Number;

	for (Count = 0; Count > Number; Count++);

	{
		cout << setw(47)  << "Enter first Number: " << endl;
		cin >> Num1;
		
		Total = Total + Num1;
	
	}
	
	cout << setw(50)  << "Total: " << Total << endl;
	
	system ("PAUSE");
	system ("cls");

	
	while (Even_Num != QUIT)
	{
		Total = Total + Num2;

		cout << "Input Next Even Number Or -1 to Quit";
			cin >> Num2;
	
	if (Num2 % 2 == 0)
	{
		Even_Num ++;
	}
	
	

	cout << "Input Next Even Number Or -1 to Quit";
			cin >> Num2;
	}  

	system ("cls");

	cout << "\n\n\n\n\n\n\n";
	cout << setw(50)  << "-------------------------"  << endl;
	cout << setw(50)  << "  Amount Of Even Number  "  << setw(3) << Even_Num << endl;
	cout << setw(50)  << "-------------------------"  << endl;

}
//End main program



tags please.
make it
1
2
3
for(Count=0; Count < Number; Count++){
//Your code here
}


Instead of
Count > Number
No Good. But thanks for looking. Any more suggestion ?
cppNoob is correct on two counts.

1: Your condition wasn't... eh... quite right...
2: You have a semicolon at line 62. Axe it!

Now, the condition on your while loop is off. Do you see why? Even_Num will never equal -1, but what will?

And get rid of the system()s. A pair of cin.ignore()s will pause the console for you. Please?

-Albatross
Yup You guys were right. Thank you so much. As for the system(cls) and system(Pause), when you say a pair of cin.ignore(?) what do you mean I think you're telling me to type cin.ignore(cls)? thank you so much again.
system("pause");
can be replaced with
cin.ignore(); cin.ignore();

There is no real standard-library replacement for system("cls");, although maybe you could try:
1
2
for(int i = 0; i < 128; i++)
    cout << endl;


We have good reasons for discouraging the use of system, by the way. If you check the Articles section of this forum, there's a stickied post about console programming with a link to why system() is "evil".

-Albatross
Last edited on
Topic archived. No new replies allowed.