I need help with For Loop

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;
}
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. 
		
	}
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;
Yayyyy thank you so much, i really i appreciate that!
Also thanks @soy01
@joe864864 what is the function of toupper?
http://www.cplusplus.com/reference/cctype/toupper/
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.
@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
Topic archived. No new replies allowed.