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
#include <iostream>
#include <iomanip>
#include <cmath>
#include <string>
usingnamespace 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;
}
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.
}
#include <iostream>
#include <cctype>
#include <iomanip>
usingnamespace 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;
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?