Hi i've been having trouble with my c++ program.
instead of repeating the digits over and over, how would
I make it so the repeating digits are blank spaces with the number of the row at the end.
So instead of my program outputting:
1
22
333
I want it to output
1
--2
---3
where the "-" are blank spaces
and so on for the number of rows the reader specifies.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
int main(){
//Declare variables
int num;
//Input number of rows
cout<<" Enter the number of rows you wish to Output"<<endl;
cin>>num;
if(num<=0){
cout<<" The number cannot be a negative or 0 "<<endl;
cout<<" Enter another number "<<endl;
cin>>num;
}
for (int row = 1; row <= num; row++) {
for (int col = 1; col <= row; col++)
cout << row;
cout << endl;
}
#include <iostream>
usingnamespace std;
int main()
{
//Declare variables
int num;
//Input number of rows
cout<<" Enter the number of rows you wish to Output"<<endl;
cin>>num;
cin.ignore();
while(num<=0)
{
cout<<" The number cannot be a negative or 0 "<<endl;
cout<<" Enter another number "<<endl;
cin>>num;
cin.ignore();
}
for (int i = 0; i < num; i++)
{
cout << i + 1 << endl;
}
cin.ignore();
return 0;
}
#include <iostream>
#include <iomanip>
usingnamespace std;
int main()
{
//Declare variables
int num;
//Input number of rows
cout<<" Enter the number of rows you wish to Output: ";
cin>>num;
cout << endl;
cin.ignore();
while(num<=0)
{
cout<<" The number cannot be a negative or 0 "<<endl;
cout<<" Enter another number: ";
cin>>num;
cout << endl;
cin.ignore();
}
for (int i = 0; i < num; i++)
{
cout << setw(i + 1) << i + 1 << endl;
}
cin.ignore();
return 0;
}
the std:: in front is called scoping it I put that because I didn't include the std namespace. The reason yours is messed up is because you put an endl; You don't want to put an endl; You are outputting the spaces before the number.