Oct 25, 2009 at 12:34am UTC
my prof wants us to write a program that accepts positive integers and a character input then when the program outputs what you entered there are only 10 characters per line.
this is what i have so far
#include <iostream>
using namespace std;
int main() {
int num;
char character;
cout << "give me a positive integer ";
cin >> num;
cout << " give me a character";
cin>> character;
for (int j=0; j<num; j=j+1)
{
cout << character;
}
cout << endl;
return 0;
}
if i input 15 and b
it outputs
bbbbbbbbbbbbbbb
i need it to output
bbbbbbbbbb
bbbbb
can anyone help if been trolling forum afte forum trying to figure it out
Oct 25, 2009 at 12:38am UTC
i have figured more of it out but im not sure if it is right?
using namespace std;
int main()
{
char character;
int num;
int x= 9;
cout << "Enter a positive integer: " << endl;
cin >> num;
cout << "Enter a character: " << endl;
cin >> character;
cout << "The positive integer entered was " << num << endl;
cout << "The character entered is " << character << endl;
for (int j=0; j<x; j++)
{
cout << character;
}
if (num> 0)
{
while (num> 0)
{
cout << character << endl;
num= num-1;
}
}
else cout << " Thats an incorrect number!" << endl;
return 0;
}
now it outputs
bbbbbbbbbb
b
b
b
b
b
b
b
b
b
b
b
b
b
b
Oct 25, 2009 at 12:57am UTC
Use a for() loop. If the line count (j in your case) is 9, 18, 27, etc (10th character in a line), print a new line.
Hint: use modulo
Oct 25, 2009 at 5:28am UTC
ok im close to having my program print out the right thing now the only problem is that it prints out like
b
bbbbbbbbbb
bbbb
how do i get that first b to fall into place correctly
my code now looks 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
int main()
{
int num;
char character;
cout << "give me a positive integer " ;
cin >> num;
cout << " give me a character" ;
cin>> character;
if (num > 0)
{
for (int N = 0; N < num; N++)
{
std::cout << character;
if (N % 10 == 0)
std::cout << endl;
}
}
else cout << "that isnt a valid integer" << endl;
return 0;
}
Last edited on Oct 25, 2009 at 5:29am UTC
Oct 25, 2009 at 6:16am UTC
Change N to 1 and N <= num.
0 modulo 0 is always 0.
Oct 25, 2009 at 6:58am UTC
already got it running thanks for your help