Student Programmer

I am writing a program for class and I can't figure out what i did wrong. Its two loops.The first loop is a counter and the second is asking the user if they want to try again. Both the for loop for the counter and my do while loop work the first time. When it loops back again after asking the user if they want to try again and they hit y and enter it will ask the user when to stop but never counts. you can run it to see what I am talking about. Here's my code.

#include <iostream>

using namespace std;
int main()
{

/***************************************************************
* First Program: Sum of Numbers- asks for user input then uses *
* a loop to stop at what ever number the user inputs. *
***************************************************************/

//Variables.
char again;
int num, count = 1;
do
{
//Asking the user for the number to stop the count at.
cout << "Enter the number were you want the count to stop." << endl;
cin >> num;

//Input validation no numbers less than 1.
if (num < 1)
{ cout << "You cannot use a negative number." << endl;
}

//For loop that iterates until it reaches the number the user put in.
for (count; count <= num; count++)
{cout << count << endl;
}

// Asks the user if they want to try again.
cout << "Would you like to try again? (y or n)";
cin >> again;
}while (again == 'Y' || again == 'y');

system("pause");
return 0;
}
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
#include <iostream>

using namespace std;
int main()
{

/***************************************************************
* First Program: Sum of Numbers- asks for user input then uses *
* a loop to stop at what ever number the user inputs. *
***************************************************************/

//Variables.
char again;
int num, count = 1;
do
{
count=1;// <<<<<<<<<<<<<< here
//Asking the user for the number to stop the count at.
cout << "Enter the number were you want the count to stop." << endl;
cin >> num;

//Input validation no numbers less than 1.
if (num < 1)
{	cout << "You cannot use a negative number." << endl;
}

//For loop that iterates until it reaches the number the user put in.
for (count; count <= num; count++)
{cout << count << endl;
}

// Asks the user if they want to try again.
cout << "Would you like to try again? (y or n)";
cin >> again;
}while (again == 'Y' || again == 'y');

system("pause");
return 0;
}

you should reset the count :PP
How do I do that?
its at topnik1's code:

char again;
int num, count = 1;
do
{


count=1;// <<<<<<<<<<<<<< here . This one.


//Asking the user for the number to stop the count at.
cout << "Enter the number were you want the count to stop." << endl;
cin >> num;


by initializing count as one (1) again, it disregards the previous value so that it wont affect the new value input by the user. you could also just use count in the for loop and just change it to:

1
2
3
for (int count = 1; count <= num; count++) /// initialize count back to 1.
{cout << count << endl;
}



They already fixed that error in the code where the <<<<<<<<<<<<<<<<<< here bit is, however, you also need to put the entire sequence of asking a user for input and then validating it, into a loop. At the moment, if I were to put in -1, it will tell me I can't put in negative numbers and then just run through the for loop anyway...
I fixed thank you. You guys are a big help!!!!
give a value to count variable inside the do.
1
2
3
 int num, count;
 do{
    count=1;
Last edited on
Topic archived. No new replies allowed.