# Beginner Problem - single digit to two digit increment problem

Printable View

• 11-12-2009
Onii
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
• 11-12-2009
darren78
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?
• 11-13-2009
Onii
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?
• 11-13-2009
darren78
Quote:

Originally Posted by Onii
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.
• 11-13-2009
darren78
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.
• 11-13-2009
rogster001
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```
• 11-13-2009
Onii
Thanks a lot for your help! :)

One thing though, why shouldnt you return main?
• 11-13-2009
rogster001
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 ;-)
• 11-13-2009
Onii
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 :p Anyhow, thanks a lot, i'll figure it out from this point on ;)

Onii
• 11-13-2009
Onii
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 :p 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-13-2009
rogster001
i meant wrong in the sense of..'these sort of things should not be allowed to happen...' ;->

Quote:

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       } }```
• 11-13-2009
CornedBee
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.)
• 11-13-2009
Onii
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
• 11-13-2009
rogster001
Quote:

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...'