8 Replies - 7173 Views - Last Post: 13 December 2010 - 10:05 AM Rate Topic: -----

#1 jun_brent19  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 12-December 10

Goto Statement in dev-c++

Posted 12 December 2010 - 04:46 PM

Gud day!,

Hi,

I just want to ask a favor on how to solve the error of my program, in my program their is a question answerable by yes or no, if you select y it should be goto to input another amount to be converted and if n it will goto to the main menu. These two option does not work below is my program and hope you could help me to work this program

Thanks and regards,
Jun

a

*** Moderator error ** I somehow deleted you code when I went to put in the code tags. Please post your code again. be sure to use code tags!

This post has been edited by NickDMax: 12 December 2010 - 05:06 PM
Reason for edit:: added code tags: [code] -- paste your code here -- [/code]


Is This A Good Question/Topic? 0
  • +

Replies To: Goto Statement in dev-c++

#2 Alex6788  Icon User is offline

  • kitties == adorable


Reputation: 144
  • View blog
  • Posts: 1,667
  • Joined: 15-July 10

Re: Goto Statement in dev-c++

Posted 12 December 2010 - 04:54 PM

Post your code in code tags :code:

Do you have to use goto statements? Gotos aren't the best thing to use.

Also the conio library is old and outdated.

This post has been edited by Alex6788: 12 December 2010 - 04:55 PM

Was This Post Helpful? 0
  • +
  • -

#3 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2870
  • View blog
  • Posts: 11,021
  • Joined: 15-July 08

Re: Goto Statement in dev-c++

Posted 12 December 2010 - 05:00 PM

You can do anything you want in C++ without using the goto statement and it is terrible practice to do so (with a controversial exception that Linus Torvolds explains). Use if statements and loops instead. ANYTHING but gotos.
Was This Post Helpful? 0
  • +
  • -

#4 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: Goto Statement in dev-c++

Posted 12 December 2010 - 05:10 PM

Sorry jun_brent19, I messed up your post and removed the code when I tried to add in code tags. Can you please repost your code (using code tags).
Was This Post Helpful? 0
  • +
  • -

#5 jun_brent19  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 12-December 10

Re: Goto Statement in dev-c++

Posted 12 December 2010 - 05:57 PM

hi nickDmax,

here is your request...hope to receive immediate solution to this problem..
best regards,
junbrent

# include <iostream>
# include <conio.h>
using namespace std;
float val, b, c, d, e, f, g, pes, dol, yen, ypes, upes,eur;

char sel;

int main()
{
main:  
       
       
    system ("cls");
    cout<<"====================================================================\n"<<endl;
     
    cout<<"====================================================================\n"<<endl;
    cout<<"[1] - Convert Peso to Dollar:"<<endl;
    cout<<"[2] - Convert Dollar to Peso:"<<endl;
    cout<<"[3] - Convert Peso to Yen:"<<endl;
    cout<<"[4] - Convert Yen to Peso:"<<endl;
    cout<<"[5] - Convert Peso to Euro:"<<endl;
    cout<<"[6] - Convert Euro to Peso:"<<endl;
    cout<<"[7]- Exit:"<<endl;
    
    cout<<"====================================================================\n"<<endl;
     
    cout<<"====================================================================\n"<<endl;
    
    cout<<"Please enter your selection:";
    cin>> sel;
    
    switch (sel)
    {
                case '1':
                bb:  
    
                system ("cls");
                cout<<"Convert Peso to Dollar:"; 
                cout<<"\nPlease enter the amount in Peso:";    
                cin>>val;
                dol = val*44;
                cout<<"The amount in Dollar:";    
                cout<<dol;
                cout<<endl;
                cout<<"You want to enter another amount? y/n:";      
                cin>>b;  
                if((b=='y')||(b=='Y'))
                goto bb;
                else if((b=='n')||(b=='N'))
                goto main;
                
                break;
               
                case '2':
                cc: 
                system ("cls");
                cout<<"Convert Dollar to Peso:"; 
                cout<<"\nPlease enter the amount in Peso:";    
                cin>>val;
                pes = val/44;
                cout<<"The amount in Dollar:";    
                cout<<pes;
                cout<<endl;
                cout<<"You want to enter another amount? y/n:";      
                cin>>b;    
                if((b=='y')||(b=='Y'))
                goto cc;
                else if((b=='n')||(b=='N'))
                goto main;
                     
               break;
               
               case '3':
               dd:
               system ("cls");
                cout<<"Convert Peso to Yen:"; 
                cout<<"\nPlease enter the amount in Peso:";    
                cin>>val;
                yen = val*55;
                cout<<"The amount in Yen:";    
                cout<<yen;
                cout<<endl;
                cout<<"You want to enter another amount? y/n:";      
                cin>>b;    
                if((b=='y')||(b=='Y'))
                goto dd;
                else if((b=='n')||(b=='N'))
                goto main;
               
                
                
               break;
               
               case '4':
                ee:
                system ("cls");
                cout<<"Convert Yen to Peso:"; 
                cout<<"\nPlease enter the amount in Yen:";    
                cin>>val;
                ypes = val/55;
                cout<<"The amount in Peso:";    
                cout<<ypes;
                cout<<endl;
                cout<<"You want to enter another amount? y/n:";      
                cin>>b;    
                if((b=='y')||(b=='Y'))
                goto ee;
                else if((b=='n')||(b=='N'))
                goto main;
              
                break;
                
                case '5':
                ff:
                system ("cls"); 
                cout<<"Convert Peso to Euro:"; 
                cout<<"\nPlease enter the amount in Euro:";    
                cin>>val;
                eur = val*70;
                cout<<"The amount in eur:";    
                cout<<eur;
                cout<<endl;
                cout<<"You want to enter another amount? y/n:";      
                cin>>b;    
                if((b=='y')||(b=='Y'))
                goto ff;
                else if((b=='n')||(b=='N'))
                goto main;
               
    
                 break;
    
                case '6':
                gg:
                system ("cls");  
                cout<<"Convert Euro to Peso:"; 
                cout<<"\nPlease enter the amount in Euro:";    
                cin>>val;
                upes = val/70;
                cout<<"The amount in eur:";    
                cout<<upes;
                cout<<endl;
                cout<<"You want to enter another amount? y/n:";      
                cin>>b;    
                if((b=='y')||(b=='Y'))
                goto gg;
                else if((b=='n')||(b=='N'))
                goto main;
              
                
                 break;
                 case '7':
                hh:
                system ("cls");  
                cout<<"Exit:"; 
                cout<<endl;    
                
            
                break;
               default:
     
             cout<<"Your selection is Wrong!:";
             getch();
             }
             return 0;
    
              }


MOD EDIT: Way to ignore the code tags request! When posting code...USE CODE TAGS!!!

:code:

This post has been edited by JackOfAllTrades: 12 December 2010 - 06:21 PM

Was This Post Helpful? 0
  • +
  • -

#6 youbob1212  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 16
  • View blog
  • Posts: 130
  • Joined: 31-October 10

Re: Goto Statement in dev-c++

Posted 13 December 2010 - 12:06 AM

"Goto" is evil. Reason why, is that it makes it very hard for other programmers to know what's your direction in your code..Far as how it flows. Thus your code will turn into spaghetti in no time.

Drop the goto and use Loop statements like "while", "do while", "for".

To solve your problem in this. I think all you have is start a huge loop and then nest loops inside your main loop.
Was This Post Helpful? 1
  • +
  • -

#7 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: Goto Statement in dev-c++

Posted 13 December 2010 - 09:10 AM

So basically your program is the antithesis of "good programming" -- I do not mean this as an insult - you have done many things that are considered "poor programming" but they are all things that I did as a beginner myself so PLEASE don't be offended.

Lesson #1: Try to avoid using global variables. In your program they do not help or hurt but overall they are generally a bad idea and you should avoid using them if possible. Always try to keep your data close to where it is being used.

That means put your variables INSIDE of main() (or other functions) not before main().

Lesson #2: Use control structures over goto's -- Your solution of using the gotos is actually kind of neat but as you progress a programmer you need to learn to move away from these solutions.

However, this is not your problem. The problem is that you are reading in b (which is a float) and then comparing it with a char ('y', 'Y', 'n', 'N') -- so the user would have to enter the ascii value of of the char.

Try your program and enter 89 rather than 'Y' and that should work.

OR, change b to a char.

here is a fix for your problem... note that I left in the goto's -- I also added a little function to clear out the input buffer which will help a little with some errors.

# include <iostream>
# include <limits>
# include <conio.h>
using namespace std;
float val, c, d, e, f, g, pes, dol, yen, ypes, upes, eur;

char sel, b;

void clearCin() { cin.ignore(numeric_limits<streamsize>::max(),'\n'); cin.clear(); }

int main() {
main:
    system ("cls");
    cout << "====================================================================\n" << endl;
    cout << "====================================================================\n" << endl;
    cout << "[1] - Convert Peso to Dollar:" << endl;
    cout << "[2] - Convert Dollar to Peso:" << endl;
    cout << "[3] - Convert Peso to Yen:" << endl;
    cout << "[4] - Convert Yen to Peso:" << endl;
    cout << "[5] - Convert Peso to Euro:" << endl;
    cout << "[6] - Convert Euro to Peso:" << endl;
    cout << "[7]- Exit:" << endl;
    cout << "====================================================================\n" << endl;
    cout << "====================================================================\n" << endl;
    cout << "Please enter your selection:";
    cin >> sel;
    //clearCin();

    switch (sel) {
        case '1':
bb:
            system ("cls");
            cout << "Convert Peso to Dollar:";
            cout << "\nPlease enter the amount in Peso:";
            cin >> val;
            dol = val * 44;
            cout << "The amount in Dollar:";
            cout << dol;
            cout << endl;
            cout << "You want to enter another amount? y/n:";
            cin >> b;
            clearCin();
            if ((b == 'y') || (b == 'Y'))
                goto bb;
            else if ((b == 'n') || (b == 'N'))
                goto main;

            break;
        case '2':
cc:
            system ("cls");
            cout << "Convert Dollar to Peso:";
            cout << "\nPlease enter the amount in Peso:";
            cin >> val;
            pes = val / 44;
            cout << "The amount in Dollar:";
            cout << pes;
            cout << endl;
            cout << "You want to enter another amount? y/n:";
            cin >> b;
            clearCin();
            if ((b == 'y') || (b == 'Y'))
                goto cc;
            else if ((b == 'n') || (b == 'N'))
                goto main;

            break;
        case '3':
dd:
            system ("cls");
            cout << "Convert Peso to Yen:";
            cout << "\nPlease enter the amount in Peso:";
            cin >> val;
            yen = val * 55;
            cout << "The amount in Yen:";
            cout << yen;
            cout << endl;
            cout << "You want to enter another amount? y/n:";
            cin >> b;
            clearCin();
            if ((b == 'y') || (b == 'Y'))
                goto dd;
            else if ((b == 'n') || (b == 'N'))
                goto main;

            break;
        case '4':
ee:
            system ("cls");
            cout << "Convert Yen to Peso:";
            cout << "\nPlease enter the amount in Yen:";
            cin >> val;
            ypes = val / 55;
            cout << "The amount in Peso:";
            cout << ypes;
            cout << endl;
            cout << "You want to enter another amount? y/n:";
            cin >> b;
            clearCin();
            if ((b == 'y') || (b == 'Y'))
                goto ee;
            else if ((b == 'n') || (b == 'N'))
                goto main;

            break;
        case '5':
ff:
            system ("cls");
            cout << "Convert Peso to Euro:";
            cout << "\nPlease enter the amount in Euro:";
            cin >> val;
            eur = val * 70;
            cout << "The amount in eur:";
            cout << eur;
            cout << endl;
            cout << "You want to enter another amount? y/n:";
            cin >> b;
            clearCin();
            if ((b == 'y') || (b == 'Y'))
                goto ff;
            else if ((b == 'n') || (b == 'N'))
                goto main;

            break;
        case '6':
gg:
            system ("cls");
            cout << "Convert Euro to Peso:";
            cout << "\nPlease enter the amount in Euro:";
            cin >> val;
            upes = val / 70;
            cout << "The amount in eur:";
            cout << upes;
            cout << endl;
            cout << "You want to enter another amount? y/n:";
            cin >> b;
            clearCin();
            if ((b == 'y') || (b == 'Y'))
                goto gg;
            else if ((b == 'n') || (b == 'N'))
                goto main;

            break;
        case '7':
hh:
            system ("cls");
            cout << "Exit:";
            cout << endl;
            break;
        default:
            cout << "Your selection is Wrong!:";
            getch();
    }

    return 0;
}

Was This Post Helpful? 0
  • +
  • -

#8 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: Goto Statement in dev-c++

Posted 13 December 2010 - 09:27 AM

here is a version with out using the goto's or global variables:
# include <iostream>
# include <limits>
# include <conio.h>
using namespace std;

inline void clearCin() {
    cin.ignore(numeric_limits<streamsize>::max(), '\n');
    cin.clear();
}

int main() {
    float val, c, d, e, f, g, pes, dol, yen, ypes, upes, eur;
    char sel, b;
    bool exitProgram = false;

    while (!exitProgram) {
        system ("cls");
        cout << "====================================================================\n" << endl;
        cout << "====================================================================\n" << endl;
        cout << "[1] - Convert Peso to Dollar:" << endl;
        cout << "[2] - Convert Dollar to Peso:" << endl;
        cout << "[3] - Convert Peso to Yen:" << endl;
        cout << "[4] - Convert Yen to Peso:" << endl;
        cout << "[5] - Convert Peso to Euro:" << endl;
        cout << "[6] - Convert Euro to Peso:" << endl;
        cout << "[7]- Exit:" << endl;
        cout << "====================================================================\n" << endl;
        cout << "====================================================================\n" << endl;
        cout << "Please enter your selection:";
        cin >> sel;
        //clearCin();

        switch (sel) {
            case '1':

                do {
                    system ("cls");
                    cout << "Convert Peso to Dollar:";
                    cout << "\nPlease enter the amount in Peso:";
                    cin >> val;
                    dol = val * 44;
                    cout << "The amount in Dollar:";
                    cout << dol;
                    cout << endl;
                    cout << "You want to enter another amount? y/n:";
                    cin >> b;
                    clearCin();
                } while ((b == 'y') || (b == 'Y'));

                break;
            case '2':

                do {
                    system ("cls");
                    cout << "Convert Dollar to Peso:";
                    cout << "\nPlease enter the amount in Peso:";
                    cin >> val;
                    pes = val / 44;
                    cout << "The amount in Dollar:";
                    cout << pes;
                    cout << endl;
                    cout << "You want to enter another amount? y/n:";
                    cin >> b;
                    clearCin();
                } while ((b == 'y') || (b == 'Y'));

                break;
            case '3':

                do {
                    system ("cls");
                    cout << "Convert Peso to Yen:";
                    cout << "\nPlease enter the amount in Peso:";
                    cin >> val;
                    yen = val * 55;
                    cout << "The amount in Yen:";
                    cout << yen;
                    cout << endl;
                    cout << "You want to enter another amount? y/n:";
                    cin >> b;
                    clearCin();
                } while ((b == 'y') || (b == 'Y'));

                break;
            case '4':

                do {
                    system ("cls");
                    cout << "Convert Yen to Peso:";
                    cout << "\nPlease enter the amount in Yen:";
                    cin >> val;
                    ypes = val / 55;
                    cout << "The amount in Peso:";
                    cout << ypes;
                    cout << endl;
                    cout << "You want to enter another amount? y/n:";
                    cin >> b;
                    clearCin();
                } while ((b == 'y') || (b == 'Y'));

                break;
            case '5':

                do {
                    system ("cls");
                    cout << "Convert Peso to Euro:";
                    cout << "\nPlease enter the amount in Euro:";
                    cin >> val;
                    eur = val * 70;
                    cout << "The amount in eur:";
                    cout << eur;
                    cout << endl;
                    cout << "You want to enter another amount? y/n:";
                    cin >> b;
                    clearCin();
                } while ((b == 'y') || (b == 'Y'));

                break;
            case '6':

                do {                
                    system ("cls");
                    cout << "Convert Euro to Peso:";
                    cout << "\nPlease enter the amount in Euro:";
                    cin >> val;
                    upes = val / 70;
                    cout << "The amount in eur:";
                    cout << upes;
                    cout << endl;
                    cout << "You want to enter another amount? y/n:";
                    cin >> b;
                    clearCin();
                } while ((b == 'y') || (b == 'Y'));

                break;
            case '7':
                system ("cls");
                cout << "Exit:";
                cout << endl;
                exitProgram = true;
                break;
            default:
                cout << "Your selection is Wrong!:";
                getch();                
        }
    }

    return 0;
}




the code still have some problems with verious inputs but if the user plays nice the program works
Was This Post Helpful? 0
  • +
  • -

#9 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5761
  • View blog
  • Posts: 12,574
  • Joined: 16-October 07

Re: Goto Statement in dev-c++

Posted 13 December 2010 - 10:05 AM

Use methods. Use lots of methods. Use no gotos, never, none, nada.

Your conversion script repeats; a lot. Generalize that an make a method. You did it once with clearCin().

e.g.
#include <iostream>
#include <limits>

using namespace std;

void clearCin() {
	cin.ignore(numeric_limits<streamsize>::max(),'\n');
	cin.clear();
}


bool anotherAmount() {
	cout << endl;
	cout << "You want to enter another amount? y/n:";
	char b;
	cin >> b;
	clearCin();
	return (b == 'y') || (b == 'Y');
}

char menu() {
	const char *bigBar = "====================================================================";
	cout << bigBar << endl << bigBar << endl;
	cout << "[1] - Convert Peso to Dollar:" << endl;
	cout << "[2] - Convert Dollar to Peso:" << endl;
	cout << "[3] - Convert Peso to Yen:" << endl;
	cout << "[4] - Convert Yen to Peso:" << endl;
	cout << "[5] - Convert Peso to Euro:" << endl;
	cout << "[6] - Convert Euro to Peso:" << endl;
	cout << "[7]- Exit:" << endl;
	cout << bigBar << endl << bigBar << endl;
	
	char sel;
	cout << "Please enter your selection:";
	cin >> sel;
	clearCin();
	return sel;
}

bool optionConvert(const char *from, const char *to, float convert) {
	cout << "Convert " << from << " to " << to << ":";
	cout << "\nPlease enter the amount in " << from << ":";
	float val;
	cin >> val;
	float amount = val * convert;
	cout << "The amount in " << to << ":" << amount << endl;
	return anotherAmount();
}

bool programBody() {
	bool keepGoing = true;
	switch (menu()) {
		case '1': while(optionConvert("Peso", "Dollar", 44)); break;
		case '2': while(optionConvert("Dollar", "Peso", 1.0/44)); break;
		case '3': case '4': case '5': case '6': cout << "Coming soon!" << endl; break;
		case '7':
			keepGoing = false;
			break;
		default:
			cout << "Your selection is Wrong!:" << endl;
			cin.get();
	}
	return keepGoing;
}

int main() {
	while(programBody());
	cout << "Exit:" << endl;
	return 0;
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1