Stuck on C++ Problem
Results 1 to 10 of 10

Thread: Stuck on C++ Problem

  1. #1
    IT Specialist Ghost_25inf's Avatar
    Join Date
    Sep 2001
    Location
    Michigan
    Posts
    648

    Question 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.
    S25vd2xlZGdlIGlzIHBvd2VyIQ

  2. #2
    Junior Member
    Join Date
    Feb 2003
    Posts
    1

    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

  3. #3
    IT Specialist Ghost_25inf's Avatar
    Join Date
    Sep 2001
    Location
    Michigan
    Posts
    648
    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
    S25vd2xlZGdlIGlzIHBvd2VyIQ

  4. #4
    Senior Member
    Join Date
    Sep 2001
    Posts
    1,027
    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
    Credit travels up, blame travels down -- The Boss

  5. #5
    IT Specialist Ghost_25inf's Avatar
    Join Date
    Sep 2001
    Location
    Michigan
    Posts
    648
    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
    S25vd2xlZGdlIGlzIHBvd2VyIQ

  6. #6
    Senior Member
    Join Date
    Sep 2001
    Posts
    1,027
    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..
    Credit travels up, blame travels down -- The Boss

  7. #7
    () \/V |\| 3 |) |3\/ |\|3G47|\/3
    Join Date
    Sep 2002
    Posts
    744
    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; 
    }

    Go Finland!
    Deviant Gallery

  8. #8
    IT Specialist Ghost_25inf's Avatar
    Join Date
    Sep 2001
    Location
    Michigan
    Posts
    648
    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?
    S25vd2xlZGdlIGlzIHBvd2VyIQ

  9. #9
    Senior Member
    Join Date
    Jun 2002
    Posts
    394
    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'
    }
    Hmm...theres something a little peculiar here. Oh i see what it is! the sentence is talking about itself! do you see that? what do you mean? sentences can\'t talk! No, but they REFER to things, and this one refers directly-unambigeously-unmistakably-to the very sentence which it is!

  10. #10
    IT Specialist Ghost_25inf's Avatar
    Join Date
    Sep 2001
    Location
    Michigan
    Posts
    648
    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.
    S25vd2xlZGdlIGlzIHBvd2VyIQ

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •