-
Stuck on C++ Problem
Let me give you some of the details of what i am tring to do here.
I'm creating a mult. table. the program will prompt users to enter a table from 2 to 12, it will also ask if you want to run the program again. this is where I get lost. This is what I have so far.
# include <iostream>
# include <iomanip>
using namespace std;
int main ()
{
int table;
cout << "What size table would you like (2 to 12)? ";
cin >> table;
cout << " | ";
for (int a=0; a <=table; a++)
cout << setw (5) << a;
cout << endl;
cout << "------------------------------------------------------------------------------------------" << endl;
for (int across=0; across <=table; across++)
{
cout << setw (3) << across << setw (3) << " | " << ' ';
for (int down=0; down <=table; down++)
cout << setw (5) << down * across;
cout << endl;
}
return 0;
}
See where would I put the loop statement in and which one should I use?
I ran is so I know this part works.
Thank you all.
-
Trying to help
It's been a while since I did C++, but if you're trying to loop it until the user wants to exit, just do-while loop it, ********** denotes added code
# include <iostream>
# include <iomanip>
using namespace std;
int main ()
{
int table;
int number; **********
Do **********
{
cout << "What size table would you like (2 to 12)? ";
cin >> table;
cout << " | ";
for (int a=0; a <=table; a++)
cout << setw (5) << a;
cout << endl;
cout << "------------------------------------------------------------------------------------------" << endl;
for (int across=0; across <=table; across++)
{
cout << setw (3) << across << setw (3) << " | " << ' ';
for (int down=0; down <=table; down++)
cout << setw (5) << down * across;
cout << endl;
cout << "do this again? (No = 1 Yes = 1)";*********
cin >> number;*********
} while (number = 0);********
}
return 0;
you can do this with words also ('y' || 'Y') but it depends on the person
-
coming up with errors, does
cout << "do this again? (no=1 yes=1)"; look right?
or should it be (no = 0 and yes = 1)
error given is syntax error return
and line 28 function should return a value void return type assumed
-
Code:
# include <iostream>
# include <iomanip>
using namespace std;
int main ()
{
char cStop[1];
int table;
do
{
cout << "What size table would you like (2 to 12)? ";
cin >> table;
cout << " | ";
for (int a=0; a <=table; a++)
cout << setw (5) << a;
cout << endl;
cout <<"------------------------------------------------------------------------------------------" << endl;
for (int across=0; across <=table; across++)
{
cout << setw (3) << across << setw (3) << " | " << ' ';
for (int down=0; down <=table; down++)
cout << setw (5) << down * across;
cout << endl;
}
cout << "Do you want to continue? (y/n): ";
cin >> cStop;
}
while( strcmp( cStop, "n" ) );
return 0;
}
bombbomb's code has 2 little errors:
"Do" should be "do" : (C/C++ is case sensitive)
"while( number = 0)" should be "while( number == 0)"
Ammo
-
strcmp what does this mean?
and what does [1] the brackets mean? I'm just tring to understand, I need to know why it works this way. otherwise I ll never learn. LOL it
-
ok,
the [1] means you are declaring a array of characters of size 1,
the strcmp is a function that compares a string (ie: an array of char) to another.
I could also have used
#include <string>
String sStop;
while( sStop != "n" )
string is a class that does strings in a "beautifuler" way...
Ammo
heh, well, actually, I was dumb:
I didn't need the [1] and the strcmp.. (duh),
a simple char cStop and while( cStope != 'n' ) would have done..
-
Here is a different way to do the same thing. The only thing I added that you did not have was a 'clear screen' command. When running a program over and over it can get a little messy having everything on one screen.
Code:
# include <iostream>
# include <iomanip>
using namespace std;
int main ()
{
int table;
char runAgain = 'y';
do
{
cout << "What size table would you like (2 to 12)? ";
cin >> table;
cout << " | ";
for (int a=0; a <=table; a++)
cout << setw (5) << a;
cout << endl;
cout << "----------------------------------------------------" << endl;
for (int across=0; across <=table; across++)
{
cout << setw (3) << across << setw (3) << " | " << ' ';
for (int down=0; down <=table; down++)
cout << setw (5) << down * across;
cout << endl;
}
cout << "Run again?: ";
cin >> runAgain;
system("cls");
}
while (runAgain == 'y' || runAgain == 'Y');
return 0;
}
-
Ok heres another question I have?
I need to produce something like this:
integer sum factorial
1 1 1
2 3 2
3 6 6
4 10 24
5 15 120
6 21 720
and so on untill it hits the count: count meaning value that was entered by user.
I can see sum is added by the next integer to get next sum and the same goes for factorial. just wondering how I sould go about starting this one?
I know I should
# include <iostream>
usning namespace std;
int main ()
{
int sum, fact, value;
cout << "What upper limit would you like? ";
cin >> count;
Sould I include math in this? and what sould I use an if statement or a for statement?
-
just for the sake of it, on the first one...
Code:
#includes...
void main() {
char repeat_program ;
for( ;; ) { //forever
. . .
do {
cout << "Want to repeat this?\n" ;
cin >> repeat_program ;
repeat_program = toupper( repeat_program ) ; //since we are including iomanip
if( repeat_program == 'Y' ) break ;
} while( repeat_program != 'N' ) ;
// force selection of 'y' or 'n'
}
-
Thank you all for the help you have provided me with. I apperciate it alot. Still haveing problems with the last one if anyone wants to take a stabe at it or just drop me a hint as to how I should go abouts starting this one. but again my thanks to you all Greenies coming your way.
I joined collage to be in networking and here I am programming, go figure that one out.