My goal is to get a starting number and an ending number from a user. The numbers are between 0-9. If the starting number is 1 then 1 is printed once. If the ending number is 4 then 4 is printed four times. Within the numbers in this example 2 is printed twice and 3 is printed three times.
I want to use nested for loops to create the download increasing rows and have the numbers go from 0-9. I also want to make sure the user only inputs positive numbers and that start is less than end. I want to tell the user the start number and the end number and display the rows of numbers. I'm taking a class right now and the teacher doesn't really go over how to incorporate new topics into the code so I could have my code structure completely wrong. I'm still trying to learn the fundamental stuff. Sorry if I miss something easy or left out some important info. I'm a little sleep deprived at the moment.
#include <iostream> // Preprocessor directive for cin and cout
usingnamespace std;
double user_start, user_end; // Variables for user input
int i;
cout << "Enter a starting number and an ending number from 0-9." << endl; // Ask user for two values
cin >> user_start >> user_end >> endl;
if (start < 0 || end > 10) // Trying make it so only positve values are allowed
{
cout << "Enter a positive number" << endl;
cin >> user_start >> user_end >> endl;
}
ifelse
{
cout << "No more chances" << endl; // Only giving the user one chance to correct his/her mistake
}
for (int i = 0; start <= end; i = i+1) // The plan is for the outer for loop to calculate the number of rows
{
for (int j = 1; j = i; j++) // The inner for loop calculates how many times i will be printed i.e. 1 once 9 nine times
{
cout << "start is 'user_start', " << "end is 'user_end': " << endl;
}
}
For one thing, you need to fix the if and ifelse statements. You need a statement to be tested for the ifelse statement. I don't really understand your logic behind that, but this is asking for for help.
Why are you using double for the two numbers? They are well withing even the range of short, and that's the smallest.
Now, the for loops are all off. You want to print one number a different number of times, correct? If so, that could be done without nesting. You need to initialize i to be 0, loop while i<user_end, and add one to i each time. This can be done with the statement for (i=0; i<user_end; i++). Then, inside the loop, output the value of user_start, if I understand your intentions clearly.
I think I could just use an else instead of an ifelse. I think the checking to make sure only positive numbers are entered should go in the for loop. The if now will test to make sure the first number entered (start number) is always less than the second number entered (end number).
The is for a class so certain things are needed, like nested for loop. Its C++ for engineers, so it might not go into programming detail as much. It's a big lecture class, so its not that easy to get help. The teacher seemed to say, up to this point, we should either use int, char, or double. I don't know what its like in other programming classes nut we didn't even tlak about namespace that much. All that was said was that it's important and must always be used.
Say the user entered numbers were 1 and 6. The output should say start is 1, end is 6 and have this structure:
1
22
333
4 4 4 4
5 5 5 5 5
6 6 6 6 6 6
So based on the numbers the user enters I should have "i" rows and each row should have "i" digits. Does that give a better picture of what I'm supposed to do?
Edit: start and end represent integer values from 0-9. I'm not sure how to allow them to represent those values and always have the start number less than the end number.
#include <iostream>
usingnamespace std;
int main()
{
int NumberA, NumberB;
cout << "\n\tEnter two numbers from 1 to 9:";
cout << "\n\t> ";
cin >> NumberA;
cout << "\n\t> ";
cin >> NumberB;
// Error checker, not very good but it's
// simple enough for you to understand:
if(NumberA > 9 || NumberA < 1 || NumberB > 9 || NumberB < 1){
cout << "Invalid Input. Ending program...";
return 0; // Just quits.
}
// This puts the larger value into NumberB
// if it wasn't already the bigger of the two:
if(NumberA > NumberB){
int Temp = NumberA;
NumberA = NumberB;
NumberB = Temp;
}
// NESTED LOOPS:
// This is to make everything loop from NumberA to NumberB:
for(NumberA; NumberA <= NumberB; NumberA++)
{
cout << "\n\t"; // spacing.
// This loop is to print out the number the correct amount of times:
for(int i = 0; i < NumberA; i++)
{
cout << NumberA;
}
}
cout << "\n\t"; // spacing so the "press anything to continue" is moved down a bit.
}
Enter two numbers from 1 to 9:
> 7
> 3
333
4444
55555
666666
7777777
Press any key to continue.
You could make a much better error guard (this one just quits if you input an incorrect number), but I don't know how much you know and didn't want there to be anything you didn't understand.