Thread: Beginner Problem - single digit to two digit increment problem

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    10

    Beginner Problem - single digit to two digit increment problem

    Heey,

    I just started programming and im trying to create programs as im learning.
    I was creating this program which would make sideway pyramids where you could choose the symbol and width of the pyramid.
    When i tried to make the program more complex, i couldn't enter a 2digit width anymore.

    If i enter a 2digit number in directy goes to:

    Code:
    cout << "\n\nPress [N] to try again." << endl;
    cout << "Press [Q] to quit." << endl;
    I tried my best to fix it but i can't.
    Here's a complete copy of my code:

    Code:
    #include <iostream>
    
    using namespace std;
    
    int a = 1;
    int b = 0;
    int d = 0;
    int x = 0;
    int width;
    char option;
    char c;
    
    int main()
    {
    	if(d == 0)
    	{
    		system("CLS");
    		cout << ":: Welcome to Sideway Piramid program ::" << endl;
    		cout << "\tChoose a letter to build te pyramid with (AaBbYyZz): ";
    		cin >> c;
    		if(c != c){return main();}
    		cout << "\n\tEnter a width (1 - 30 Characters)?: ";
    		cin >> width;
    		cin.get();
    		if(width > 30 || width < 1){return main();}
    		d = 1;
    	}
    
    	if(a == 0)
    		{
    			cout << "\n\nPress [N] to try again." << endl;
    			cout << "Press [Q] to quit." << endl;
    			cin >> option;
    			if(option == 'Q' || option == 'q'){return 0;}
    			else if(option == 'N' || option == 'n')
    			{
    				d = 0;
    				return main();
    			}
    			else
    			{
    				cout << "\n\nInvalid Key, Press [Enter] to exit.";
    				cin.get();
    				cin.get();
    				cout << "\n\nExiting...";
    				return 0;
    			}
    		}
    
    	if(a == width){b = 1;}
    
    	if(b == 0)
    	{
    		for(x = 0; x < a; x++){cout << c;}
    		a++;
    		cout << endl;
    		return main();
    	}
    
    	if(b == 1)
    	{
    		if(a == 0){d = 0;}
    		for(x = 0; x < a; x++){cout << c;}
    		a = a - 1;
    		cout << endl;
    		return main();
    	}
    }
    Hope someone could help me out, thanks in advance.

    Onii

  2. #2
    Registered User
    Join Date
    Feb 2009
    Posts
    329
    Your problem is with the double digits, its with your loop. The program only works on the first run through.

    Also, why do you keep returning main?

  3. #3
    Registered User
    Join Date
    Nov 2009
    Posts
    10
    I don't know how to make if statements run again without returning to the main function so it re-runs till it shouldn't anymore, should i use while statements instead?

  4. #4
    Registered User
    Join Date
    Feb 2009
    Posts
    329
    Quote Originally Posted by Onii View Post
    I don't know how to make if statements run again without returning to the main function so it re-runs till it shouldn't anymore, should i use while statements instead?
    I'm guessing that you mean on subsequent re runs of the program? Try encasing the whole code that you would want repeating within a while loop. You shouldn't be returning main.

  5. #5
    Registered User
    Join Date
    Feb 2009
    Posts
    329
    I would also put your quit/try again code at the end of your code. It makes more sense and is easier to read and should work better with what you are trying to do.

  6. #6
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472

    loop the loop

    psuedo example >


    Code:
    bool carry_on = true;
    
    while(carry_on)
    {
         //do stuff
    
         if(this happens)
         do this
    
         if this happens 
         do this
    
         if this happens
         carry_on = false;
    }
    
    //show quit message
    //or show quit / go again  options if the central loop is nested in a wider loop that controls the whole thing

  7. #7
    Registered User
    Join Date
    Nov 2009
    Posts
    10
    Thanks a lot for your help!

    One thing though, why shouldnt you return main?

  8. #8
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    god knows technically, but it sounds and looks sick and wrong, i cant even grasp the concept, where and from whence did you forment such an idea? i would sooner see a page full of gotos ;-)

  9. #9
    Registered User
    Join Date
    Nov 2009
    Posts
    10
    ooh lol, never thought of it being wrong, i came up with it since return 0 exits the program. Dont ask me how or why Anyhow, thanks a lot, i'll figure it out from this point on

    Onii

  10. #10
    Registered User
    Join Date
    Nov 2009
    Posts
    10
    ooh lol, never thought of it being wrong, i came up with it since return 0 exits the program. Dont ask me how or why Anyhow, thanks a lot!
    I cleaned up my code and it works with two digits now!

    I can't seem to figure out how to return to main (not return main) if i choose to make another pyramid, how do i do that?
    Here's the renewed code:

    Code:
    #include <iostream>
    
    using namespace std;
    
    int a = 1;
    int width;
    int x = 0;
    char Char;
    char option;
    bool b = false;
    bool d = true;
    
    int main()
    {
    	system("CLS");
    	cout << ":: Welcome to my Sideway Piramid counter program ::" << endl;
    	cout << "\tChoose a letter to build te pyramid with (AaBbYyZz): ";
    	cin >> Char;
    	cout << "\n\tHow wide should it be?: ";
    	cin >> width;
    	cin.ignore();
    
    	while(b == false)
    	{
    		x = 0;
    		if(a == width){b = true;}
    		while(x < a)
    		{
    			cout << Char;
    			x++;
    		}
    		a++;
    		cout << endl;
    	}
    
    
    	while(b == true)
    	{
    		x = 0;
    		if(a == 0){b = false;}
    		while(x < a)
    		{
    			cout << Char;
    			x++;
    		}
    		a = (a - 1);
    		cout << endl;
    	}
    
    	cout << "\n\nPress [N] to try again." << endl;
    	cout << "Press [Q] to quit." << endl;
    	cin >> option;
    
    	if(option == 'Q' || option == 'q'){return 0;}
    	else if(option == 'N' || option == 'n'){"????";} //What do i do here??
    	else{return 0;}
    }
    Onii

  11. #11
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    i meant wrong in the sense of..'these sort of things should not be allowed to happen...' ;->

    I can't seem to figure out how to return to main (not return main) if i choose to make another pyramid, how do i do that?
    you are already 'in' main, you have not gone anywhere, to any other functions that is, in your program all you have to worry about so far is exiting your loop when the user wants to stop the program or do something else.

    Code:
    if(option == 'Q' || option == 'q'){return 0;}
    	else if(option == 'N' || option == 'n'){"????";} //What do i do here??
    	else{return 0;}
    look at where this statement is, you will be able to enter a value and test it but then nothing else will happen, you are not in any loop to send the program flow back to the top as it were. like i originally posted if you want the user to be able to go again then you need another loop surrounding everything else, with its own control variable, i have put an example below, also i dont think you need to break up into two while loops as in your last post, you should be ablee to use one loop there and use logic if/else etc to control things.
    use exit(1) when you finally want to close the program.

    Code:
    psuedo example >
    bool go_again = true;
    bool continue_this_go = true;
    
    while(go_again)
    {
           while(continue_this_go)
           {
             //do stuff....
             //do more stuff....
    
                if(this happened)
                do this.......
    
                if(this happened)
                do this.....
    
                if(this happened)
                continue_this_go = false;
           }
    
            //show message 'do you want to try again?
            //get response
            
           if(response is yes)
           {
                continue_this_go = true;
           }
           else
           {
                go_again = false;
                exit program
           }
    }
    Last edited by rogster001; 11-13-2009 at 07:01 AM.

  12. #12
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    C++ programs aren't allowed to call main, or take its address, or do anything with it, really. main is called by the implementation at startup, and that's it.
    This rule is in place to allow certain implementation techniques for compilers. (I don't know any that make use of it, but that's a different issue.)
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  13. #13
    Registered User
    Join Date
    Nov 2009
    Posts
    10
    Thanks rogster001 and CornedBee, youve been of great help
    Its now all in one while loop (and one sub-while loop) and i used your if-else to try again or exit

    Do i need to close this topic, or is it left open?

    Onii

  14. #14
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    Do i need to close this topic, or is it left open?
    It has entered the annals of cprogramming.com for the remainder of it's natural life, until the day when it reaches nirvana, like in Logans Run movie, and goes off to forum 'a brief history of...'

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner problem with counting lines in a file
    By edd1986 in forum C Programming
    Replies: 3
    Last Post: 03-25-2005, 06:47 PM
  2. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  3. problem printf after inserting single list from file
    By jetfreggel in forum C Programming
    Replies: 3
    Last Post: 04-15-2003, 02:30 PM
  4. beginner problem
    By laasunde in forum C Programming
    Replies: 0
    Last Post: 11-21-2002, 08:24 AM
  5. beginner problem
    By The_Nymph in forum C Programming
    Replies: 4
    Last Post: 03-05-2002, 05:46 PM