I need help with For Loop

Nov 15, 2016 at 3:25am
Hi there I really need your help, I'm struggling with the for loop, the idea is user will have to enter a starting point and number of line. Then program will add 0.1 to the starting point and print out. For example
Starting point: 2
Number of line: 8
The output should be:
2.0
2.1
2.2
2.3
2.4
2.5
2.6
2.7
Here is my code so far

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
  #include <iostream>
#include <iomanip>
#include <cmath>
#include <string>
using namespace std;

int main()
{
	/*string choice;
	cout << "Would you like to begin the program? Y(es) or N(o): ";
	cin >> choice;
	while (choice == "y") {
		
	}
	cout << "You chose to quit!" << endl;*/

	double startPoint;
	int lineNumber;
	int count;
	cout << "Please enter a starting value: ";
	cin >> startPoint;
	cout << "Please enter a maximum for your table: ";
	cin >> lineNumber;
		for (count = 0 ;count < lineNumber; startPoint+= 0.1) {
			cout << startPoint << endl;
		
	}
		




	return 0;
}
Nov 15, 2016 at 3:45am
Your for loop should be:

1
2
3
4
5
6
		for (count = 0 ;count < lineNumber; count++)  //you have to count up until count is less than lineNumber
       {
			   cout << startPoint << endl;
			   startPoint+= 0.1; //this does the operation you want to do. 
		
	}
Nov 15, 2016 at 3:47am
soy01 is correct. Your code should look something like:

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
#include <iostream>
#include <cctype>
#include <iomanip>

using namespace std;

int main()
{

	double startPoint;
	int lineNumber;
	int count;
	char choice;//changed choice to a char

	cout << fixed << setprecision(1);//set to one decimal place

	cout << "Would you like to begin the program? Y(yes) or N(no): ";
	cin >> choice;
	if (toupper(choice) == 'Y')//changed to us toupper
	{
		cout << "Please enter a starting value: ";
		cin >> startPoint;
		cout << "Please enter a maximum for your table: ";
		cin >> lineNumber;
		for (count = 0; count < lineNumber; count++)//changed to count++
		{
			cout << startPoint << endl;
			startPoint += .1;//add .1 after every output

		}
	}
	else
	cout << "You chose to quit!" << endl;


	return 0;
Nov 15, 2016 at 4:57am
Yayyyy thank you so much, i really i appreciate that!
Nov 15, 2016 at 4:59am
Also thanks @soy01
@joe864864 what is the function of toupper?
Nov 15, 2016 at 5:01am
http://www.cplusplus.com/reference/cctype/toupper/
Nov 15, 2016 at 3:54pm
Toupper is used to change the character in a variable to its upper case equevelent. You have to #include <cctype> to use it. Just so you know their is also tolower to do the opposite.
Nov 16, 2016 at 8:04am
@joe844864, actually the professor requires me to use the while loop to ask the user to enter Y/N whether to continue or quit the program?If they enter y, the number will be printed and this process will be repeated until the user enter n, they the program will quit. How would i do that instead of using if/else?
Last edited on Nov 16, 2016 at 8:28am
Topic archived. No new replies allowed.