Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

returning to the beginning

i'm having trouble making my programm return to my welcome screen at the beginning of the program.
i've tried using 'goto with a start label' but then find that i can't section my program into separate bits.
this is my working (well sort of) copy at the moment.

#include
#include
#include

#define ALL_MILK_CHOCOLATE 0.25
#define SUGAR_DROPS 0.36
#define CARAMEL_SURPRISE 0.50
#define CHUNKY_MILK 0.75

char string1[]="ALL MILK CHOCOLATE";
char string2[]="SUGAR DROPS";
char string3[]="CARAMEL SURPRISE";
char string4[]="CHUNKY MILK BAR";

int num_bar; /*number of bars entered*/
float total;

welcome_screen()
{
int getch(void);

clrscr();
printf("TO BUY SOME OF OUR SCRUMPTOUS CHOCOLATE
");
printf("PRESS ANY KEY
");
getch();
return 0;
}

menu_screen()
{
int choice; /*menu selection*/
char cont; /*continue*/

clrscr();
printf("1. %s %.2fp
",string1,ALL_MILK_CHOCOLATE);
printf("2. %s %.2fp
",string2,SUGAR_DROPS);
printf("3. %s %.2fp
",string3,CARAMEL_SURPRISE);
printf("4. %s %.2fp
",string4,CHUNKY_MILK);
printf("5. CANCEL
");
printf("please enter your number selection.
when you have entered your selection press enter
");
scanf("%d",&choice);

if(choice==1)
printf("you have chosen 1. %s %.2fp
",string1,ALL_MILK_CHOCOLATE);
if(choice==2)
printf("you have chosen 2. %s %.2fp
",string2,SUGAR_DROPS);
if(choice==3)
printf("you have chosen 3. %s %.2fp
",string3,CARAMEL_SURPRISE);
if(choice==4)
printf("you have chosen 4. %s %.2fp
",string4,CHUNKY_MILK);
if(choice==5)
goto welcome_screen;(th

printf("is this correct Y/N
");
scanf("%s",&cont);
if(cont=='n')
goto welcome_screen;(THIS SEEMS TO BE THE PROBLEM)
else
clrscr();
printf("how many bars do you want maximum of 5");
scanf("%d",&num_bar);
return num_bar;
}


void main(void)
{
welcome_screen();
menu_screen();
return 0;
}
i also need to convert my choice number into the corresponding string. any ideas would be very gratefully recieved

Comments

  • stoberstober Member Posts: 9,765 ✭✭✭
    : i'm having trouble making my programm return to my welcome screen at the beginning of the program.
    : i've tried using 'goto with a start label' but then find that i can't section my program into separate bits.
    : this is my working (well sort of) copy at the moment.
    : }
    : i also need to convert my choice number into the corresponding string. any ideas would be very gratefully recieved
    :

    You can convert a number to a string like this:
    [code]
    void foo()
    {
    char buffer[18];
    int number = 5;
    sprintf(buffer,"%d", number);
    }
    [/code]

    For your first problem, you probably want to do this in the main() function. Your menu_screen() function should return a value to stop the loop.
    [code]
    void main(void)
    {
    while(1)
    {
    welcome_screen();
    if( menu_screen() == -5)
    break;
    }
    return 0;
    }
    [/code]

    For your second problem: NEVER use the goto statement! Its considered very poor coding practice, and in your program you don't need it.

    I made a few changes: (1) return -5 to stop the program. (2) changed all those if statements to a case statement. (3) removed the goto


    [code]
    int menu_screen()
    {
    int choice; /*menu selection*/
    char cont; /*continue*/

    clrscr();
    printf("1. %s %.2fp
    ",string1,ALL_MILK_CHOCOLATE);
    printf("2. %s %.2fp
    ",string2,SUGAR_DROPS);
    printf("3. %s %.2fp
    ",string3,CARAMEL_SURPRISE);
    printf("4. %s %.2fp
    ",string4,CHUNKY_MILK);
    printf("5. CANCEL
    ");
    printf("please enter your number selection.
    when you have entered your selection press enter
    ");
    scanf("%d",&choice);

    switch(choice)
    {
    case 1:
    printf("you have chosen 1. %s %.2fp
    ",string1,ALL_MILK_CHOCOLATE);
    break;
    case 2:
    printf("you have chosen 2. %s %.2fp
    ",string2,SUGAR_DROPS);
    break;
    case 3:
    printf("you have chosen 3. %s %.2fp
    ",string3,CARAMEL_SURPRISE);
    break;
    case 4:
    printf("you have chosen 4. %s %.2fp
    ",string4,CHUNKY_MILK);
    break;
    case 5:
    return -5;
    default:
    printf("is this correct Y/N
    ");
    scanf("%s",&cont);
    if(cont=='n')
    return 0;
    clrscr();
    printf("how many bars do you want maximum of 5");
    scanf("%d",&num_bar);
    return num_bar;
    }
    return 0;
    }
    [/code]


  • Geeth AsokanGeeth Asokan Member Posts: 127
    Hi,

    In order to go straight to the welcome menu, you could try a simple [b]do - while[/b] loop. In addition, using a [b]switch[/b] statement looks much better than using [b]if - else[/b] statements.

    I have modified your code to read the following:

    [code]
    #include
    #include
    #include
    #include

    void welcome_screen(void);

    #define ALL_MILK_CHOCOLATE 0.25
    #define SUGAR_DROPS 0.36
    #define CARAMEL_SURPRISE 0.50
    #define CHUNKY_MILK 0.75

    char string1[]= "ALL MILK CHOCOLATE";
    char string2[]= "SUGAR DROPS";
    char string3[]= "CARAMEL SURPRISE";
    char string4[]= "CHUNKY MILK BAR";

    /* Number of bars entered */
    int num_bar;
    float total;
    char choice;

    /* This compensates the 'enter' key */
    char buffer;
    char cont;

    /* Main function */
    int main()
    {

    /* A 'do - while' loop, which repeats the program */
    /* when the user enters 'n' */
    do
    {
    welcome_screen();

    printf("a. %s %.2fp
    ", string1 ,ALL_MILK_CHOCOLATE);
    printf("b. %s %.2fp
    ", string2 ,SUGAR_DROPS);
    printf("c. %s %.2fp
    ", string3 ,CARAMEL_SURPRISE);
    printf("d. %s %.2fp
    ", string4 ,CHUNKY_MILK);
    printf("e. CANCEL
    ");

    printf("Please enter your number selection.
    ");
    printf("When you have entered your selection, press enter: ");
    scanf("%c%c", &choice, &buffer);

    /* A 'switch' statement instead of using an 'if'
    statement */
    switch(choice)
    {
    case 'a':
    printf("
    You have chosen a. %s %.2fp
    ", string1 , ALL_MILK_CHOCOLATE);
    break;

    case 'b':
    printf("
    You have chosen b. %s %.2fp
    ", string2, SUGAR_DROPS);
    break;

    case 'c':
    printf("
    You have chosen c. %s %.2fp
    ",string3,CARAMEL_SURPRISE);
    break;

    case 'd':
    printf("
    You have chosen d. %s %.2fp
    ", string4 , CHUNKY_MILK);
    break;

    default:
    printf("
    We don't sell that! Please try again!
    ");
    printf("Or go to Tescos or Sainsburys!
    ");
    exit(0);
    break;
    }

    printf("
    Is this correct? Y/N: ");
    scanf("%c%c",&cont, &buffer);

    if(cont == 'y')
    {
    printf("
    How many bars do you want? maximum of 5: ");
    scanf("%d",&num_bar);
    }
    else
    {
    continue;
    }
    }
    while(cont != 'y');

    system("PAUSE");
    return 0;
    }

    /* Program - defined functions */

    /* Program - defined function for welcome_screen */
    void welcome_screen(void)
    {
    printf("
    TO BUY SOME OF OUR SCRUMPTOUS CHOCOLATE
    ");
    }
    [/code]

    Hope this helps,
  • nikkimarienikkimarie Member Posts: 5
    thanks. i know using goto is bad programming but i wasn't sure how else to do it.
  • nikkimarienikkimarie Member Posts: 5
    thank you as well. i still have quite a bit to do on this program so you might well see some more messages posted from me. look forward to your help in the future
  • nikkimarienikkimarie Member Posts: 5
    i've tried applying the code from both of you, but i'm still having problems. (probaly due to something silly that i am doing). anyhow when i run the program it completely misses out my switch statements.

    #include
    #include
    #include
    #include

    #define ALL_MILK_CHOCOLATE 0.25
    #define SUGAR_DROPS 0.36
    #define CARAMEL_SURPRISE 0.50
    #define CHUNKY_MILK 0.75

    char string1[]="ALL MILK CHOCOLATE";
    char string2[]="SUGAR DROPS";
    char string3[]="CARAMEL SURPRISE";
    char string4[]="CHUNKY MILK BAR";

    int num_bar; /*number of bars entered*/
    float total;

    welcome_screen()
    {
    int getch(void);

    clrscr();
    printf("TO BUY SOME OF OUR SCRUMPTOUS CHOCOLATE
    ");
    printf("PRESS ANY KEY
    ");
    getch();
    return 0;
    }

    menu_screen()
    {
    int choice; /*menu selection*/
    char cont; /*continue*/

    clrscr();
    printf("1. %s %.2fp
    ",string1,ALL_MILK_CHOCOLATE);
    printf("2. %s %.2fp
    ",string2,SUGAR_DROPS);
    printf("3. %s %.2fp
    ",string3,CARAMEL_SURPRISE);
    printf("4. %s %.2fp
    ",string4,CHUNKY_MILK);
    printf("5. CANCEL
    ");
    printf("please enter your number selection.
    ");
    printf("when you have entered your selection press enter
    ");
    scanf("%d",&choice);

    switch(choice)
    {
    case'1':
    printf("you have chosen 1. %s %.2fp
    ",string1,ALL_MILK_CHOCOLATE);
    break;
    case'2':
    printf("you have chosen 2. %s %.2fp
    ",string2,SUGAR_DROPS);
    break;
    case'3':
    printf("you have chosen 3. %s %.2fp
    ",string3,CARAMEL_SURPRISE);
    break;
    case'4':
    printf("you have chosen 4. %s %.2fp
    ",string4,CHUNKY_MILK);
    break;
    case'5':
    return -5;


    default:
    printf("is this correct Y/N
    ");
    scanf("%s",&cont);
    if(cont=='n')
    return 0;
    clrscr();
    printf("how many bars do you want maximum of 5");
    scanf("%d",&num_bar);
    return num_bar;
    }
    return 0;
    }

    void main(void)
    {
    while (1)
    {
    welcome_screen();
    if(menu_screen()== -5)
    break;
    }

    return 0;
    }


  • Geeth AsokanGeeth Asokan Member Posts: 127
    : i've tried applying the code from both of you, but i'm still having problems. (probaly due to something silly that i am doing). anyhow when i run the program it completely misses out my switch statements.
    :
    : #include
    : #include
    : #include
    : #include
    :
    : #define ALL_MILK_CHOCOLATE 0.25
    : #define SUGAR_DROPS 0.36
    : #define CARAMEL_SURPRISE 0.50
    : #define CHUNKY_MILK 0.75
    :
    : char string1[]="ALL MILK CHOCOLATE";
    : char string2[]="SUGAR DROPS";
    : char string3[]="CARAMEL SURPRISE";
    : char string4[]="CHUNKY MILK BAR";
    :
    : int num_bar; /*number of bars entered*/
    : float total;
    :
    : welcome_screen()
    : {
    : int getch(void);
    :
    : clrscr();
    : printf("TO BUY SOME OF OUR SCRUMPTOUS CHOCOLATE
    ");
    : printf("PRESS ANY KEY
    ");
    : getch();
    : return 0;
    : }
    :
    : menu_screen()
    : {
    : int choice; /*menu selection*/
    : char cont; /*continue*/
    :
    : clrscr();
    : printf("1. %s %.2fp
    ",string1,ALL_MILK_CHOCOLATE);
    : printf("2. %s %.2fp
    ",string2,SUGAR_DROPS);
    : printf("3. %s %.2fp
    ",string3,CARAMEL_SURPRISE);
    : printf("4. %s %.2fp
    ",string4,CHUNKY_MILK);
    : printf("5. CANCEL
    ");
    : printf("please enter your number selection.
    ");
    : printf("when you have entered your selection press enter
    ");
    : scanf("%d",&choice);
    :
    : switch(choice)
    : {
    : case'1':
    : printf("you have chosen 1. %s %.2fp
    ",string1,ALL_MILK_CHOCOLATE);
    : break;
    : case'2':
    : printf("you have chosen 2. %s %.2fp
    ",string2,SUGAR_DROPS);
    : break;
    : case'3':
    : printf("you have chosen 3. %s %.2fp
    ",string3,CARAMEL_SURPRISE);
    : break;
    : case'4':
    : printf("you have chosen 4. %s %.2fp
    ",string4,CHUNKY_MILK);
    : break;
    : case'5':
    : return -5;
    :
    :
    : default:
    : printf("is this correct Y/N
    ");
    : scanf("%s",&cont);
    : if(cont=='n')
    : return 0;
    : clrscr();
    : printf("how many bars do you want maximum of 5");
    : scanf("%d",&num_bar);
    : return num_bar;
    : }
    : return 0;
    : }
    :
    : void main(void)
    : {
    : while (1)
    : {
    : welcome_screen();
    : if(menu_screen()== -5)
    : break;
    : }
    :
    : return 0;
    : }
    :
    :
    :
    [blue]
    Hi,

    Your [b]switch[/b] statements have not been implemented properly. Try this code, I have modified it and it works!:

    [code]
    #include
    #include
    #include
    #include

    void welcome_screen(void);

    #define ALL_MILK_CHOCOLATE 0.25
    #define SUGAR_DROPS 0.36
    #define CARAMEL_SURPRISE 0.50
    #define CHUNKY_MILK 0.75

    char string1[]= "ALL MILK CHOCOLATE";
    char string2[]= "SUGAR DROPS";
    char string3[]= "CARAMEL SURPRISE";
    char string4[]= "CHUNKY MILK BAR";

    /* Number of bars entered */
    int num_bar;
    float total;
    char choice;

    /* This compensates the 'enter' key */
    char buffer;
    char cont;

    /* Main function */
    int main()
    {

    /* A 'do - while' loop, which repeats the program */
    /* when the user enters 'n' */
    do
    {
    welcome_screen();

    printf("a. %s %.2fp
    ", string1 ,ALL_MILK_CHOCOLATE);
    printf("b. %s %.2fp
    ", string2 ,SUGAR_DROPS);
    printf("c. %s %.2fp
    ", string3 ,CARAMEL_SURPRISE);
    printf("d. %s %.2fp
    ", string4 ,CHUNKY_MILK);
    printf("e. CANCEL
    ");

    printf("Please enter your number selection.
    ");
    printf("When you have entered your selection, press enter: ");
    scanf("%c%c", &choice, &buffer);

    /* A 'switch' statement instead of using an 'if'
    statement */
    switch(choice)
    {
    case 'a':
    printf("
    You have chosen a. %s %.2fp
    ", string1 , ALL_MILK_CHOCOLATE);
    break;

    case 'b':
    printf("
    You have chosen b. %s %.2fp
    ", string2, SUGAR_DROPS);
    break;

    case 'c':
    printf("
    You have chosen c. %s %.2fp
    ",string3,CARAMEL_SURPRISE);
    break;

    case 'd':
    printf("
    You have chosen d. %s %.2fp
    ", string4 , CHUNKY_MILK);
    break;

    default:
    printf("
    We don't sell that! Please try again!
    ");
    printf("Or go to Tescos or Sainsburys!
    ");
    exit(0);
    break;
    }

    printf("
    Is this correct? Y/N: ");
    scanf("%c%c",&cont, &buffer);

    if(cont == 'y')
    {
    printf("
    How many bars do you want? maximum of 5: ");
    scanf("%d",&num_bar);
    }
    else
    {
    continue;
    }
    }
    while(cont != 'y');

    return 0;
    }

    /* Program - defined functions */

    /* Program - defined function for welcome_screen */
    void welcome_screen(void)
    {
    printf("
    TO BUY SOME OF OUR SCRUMPTOUS CHOCOLATE
    ");
    }
    [/code]

    Try out the above code!

    Hope this helps,

    [/blue]
  • nikkimarienikkimarie Member Posts: 5
    : : this is working better. thank you . but i need the program to return to the welcome screen if 'e' is selected and at any other time that 'no' is selected. i can do it if i use the goto but i know that is bad and should'nt be used so any help in that direction would be helpful.

    p.s. my college tutor doesn't know what he is doing and relies on work sheets to get him by. terrible isn't it.
    : :
    : :
    : [blue]
    : Hi,
    :
    : Your [b]switch[/b] statements have not been implemented properly. Try this code, I have modified it and it works!:
    :
    : [code]
    : #include
    : #include
    : #include
    : #include
    :
    : void welcome_screen(void);
    :
    : #define ALL_MILK_CHOCOLATE 0.25
    : #define SUGAR_DROPS 0.36
    : #define CARAMEL_SURPRISE 0.50
    : #define CHUNKY_MILK 0.75
    :
    : char string1[]= "ALL MILK CHOCOLATE";
    : char string2[]= "SUGAR DROPS";
    : char string3[]= "CARAMEL SURPRISE";
    : char string4[]= "CHUNKY MILK BAR";
    :
    : /* Number of bars entered */
    : int num_bar;
    : float total;
    : char choice;
    :
    : /* This compensates the 'enter' key */
    : char buffer;
    : char cont;
    :
    : /* Main function */
    : int main()
    : {
    :
    : /* A 'do - while' loop, which repeats the program */
    : /* when the user enters 'n' */
    : do
    : {
    : welcome_screen();
    :
    : printf("a. %s %.2fp
    ", string1 ,ALL_MILK_CHOCOLATE);
    : printf("b. %s %.2fp
    ", string2 ,SUGAR_DROPS);
    : printf("c. %s %.2fp
    ", string3 ,CARAMEL_SURPRISE);
    : printf("d. %s %.2fp
    ", string4 ,CHUNKY_MILK);
    : printf("e. CANCEL
    ");
    :
    : printf("Please enter your number selection.
    ");
    : printf("When you have entered your selection, press enter: ");
    : scanf("%c%c", &choice, &buffer);
    :
    : /* A 'switch' statement instead of using an 'if'
    : statement */
    : switch(choice)
    : {
    : case 'a':
    : printf("
    You have chosen a. %s %.2fp
    ", string1 , ALL_MILK_CHOCOLATE);
    : break;
    :
    : case 'b':
    : printf("
    You have chosen b. %s %.2fp
    ", string2, SUGAR_DROPS);
    : break;
    :
    : case 'c':
    : printf("
    You have chosen c. %s %.2fp
    ",string3,CARAMEL_SURPRISE);
    : break;
    :
    : case 'd':
    : printf("
    You have chosen d. %s %.2fp
    ", string4 , CHUNKY_MILK);
    : break;
    :
    : default:
    : printf("
    We don't sell that! Please try again!
    ");
    : printf("Or go to Tescos or Sainsburys!
    ");
    : exit(0);
    : break;
    : }
    :
    : printf("
    Is this correct? Y/N: ");
    : scanf("%c%c",&cont, &buffer);
    :
    : if(cont == 'y')
    : {
    : printf("
    How many bars do you want? maximum of 5: ");
    : scanf("%d",&num_bar);
    : }
    : else
    : {
    : continue;
    : }
    : }
    : while(cont != 'y');
    :
    : return 0;
    : }
    :
    : /* Program - defined functions */
    :
    : /* Program - defined function for welcome_screen */
    : void welcome_screen(void)
    : {
    : printf("
    TO BUY SOME OF OUR SCRUMPTOUS CHOCOLATE
    ");
    : }
    : [/code]
    :
    : Try out the above code!
    :
    : Hope this helps,
    :
    : [/blue]
    :

  • Geeth AsokanGeeth Asokan Member Posts: 127
    [blue]
    Hi,

    You could implement a [b]goto[/b] statement in order for the program to go back to the welcome screen once the user presses 'e'.

    [/blue]

    : : : this is working better. thank you . but i need the program to return to the welcome screen if 'e' is selected and at any other time that 'no' is selected. i can do it if i use the goto but i know that is bad and should'nt be used so any help in that direction would be helpful.
    :
    : p.s. my college tutor doesn't know what he is doing and relies on work sheets to get him by. terrible isn't it.
    : : :
    : : :
    : : [blue]
    : : Hi,
    : :
    : : Your [b]switch[/b] statements have not been implemented properly. Try this code, I have modified it and it works!:
    : :
    : : [code]
    : : #include
    : : #include
    : : #include
    : : #include
    : :
    : : void welcome_screen(void);
    : :
    : : #define ALL_MILK_CHOCOLATE 0.25
    : : #define SUGAR_DROPS 0.36
    : : #define CARAMEL_SURPRISE 0.50
    : : #define CHUNKY_MILK 0.75
    : :
    : : char string1[]= "ALL MILK CHOCOLATE";
    : : char string2[]= "SUGAR DROPS";
    : : char string3[]= "CARAMEL SURPRISE";
    : : char string4[]= "CHUNKY MILK BAR";
    : :
    : : /* Number of bars entered */
    : : int num_bar;
    : : float total;
    : : char choice;
    : :
    : : /* This compensates the 'enter' key */
    : : char buffer;
    : : char cont;
    : :
    : : /* Main function */
    : : int main()
    : : {
    : :
    : : /* A 'do - while' loop, which repeats the program */
    : : /* when the user enters 'n' */
    : : do
    : : {
    : : welcome_screen();
    : :
    : : printf("a. %s %.2fp
    ", string1 ,ALL_MILK_CHOCOLATE);
    : : printf("b. %s %.2fp
    ", string2 ,SUGAR_DROPS);
    : : printf("c. %s %.2fp
    ", string3 ,CARAMEL_SURPRISE);
    : : printf("d. %s %.2fp
    ", string4 ,CHUNKY_MILK);
    : : printf("e. CANCEL
    ");
    : :
    : : printf("Please enter your number selection.
    ");
    : : printf("When you have entered your selection, press enter: ");
    : : scanf("%c%c", &choice, &buffer);
    : :
    : : /* A 'switch' statement instead of using an 'if'
    : : statement */
    : : switch(choice)
    : : {
    : : case 'a':
    : : printf("
    You have chosen a. %s %.2fp
    ", string1 , ALL_MILK_CHOCOLATE);
    : : break;
    : :
    : : case 'b':
    : : printf("
    You have chosen b. %s %.2fp
    ", string2, SUGAR_DROPS);
    : : break;
    : :
    : : case 'c':
    : : printf("
    You have chosen c. %s %.2fp
    ",string3,CARAMEL_SURPRISE);
    : : break;
    : :
    : : case 'd':
    : : printf("
    You have chosen d. %s %.2fp
    ", string4 , CHUNKY_MILK);
    : : break;
    : :
    : : default:
    : : printf("
    We don't sell that! Please try again!
    ");
    : : printf("Or go to Tescos or Sainsburys!
    ");
    : : exit(0);
    : : break;
    : : }
    : :
    : : printf("
    Is this correct? Y/N: ");
    : : scanf("%c%c",&cont, &buffer);
    : :
    : : if(cont == 'y')
    : : {
    : : printf("
    How many bars do you want? maximum of 5: ");
    : : scanf("%d",&num_bar);
    : : }
    : : else
    : : {
    : : continue;
    : : }
    : : }
    : : while(cont != 'y');
    : :
    : : return 0;
    : : }
    : :
    : : /* Program - defined functions */
    : :
    : : /* Program - defined function for welcome_screen */
    : : void welcome_screen(void)
    : : {
    : : printf("
    TO BUY SOME OF OUR SCRUMPTOUS CHOCOLATE
    ");
    : : }
    : : [/code]
    : :
    : : Try out the above code!
    : :
    : : Hope this helps,
    : :
    : : [/blue]
    : :
    :
    :

  • Justin BibJustin Bib USAMember Posts: 0

    ____ / http://forcoder.org / free video tutorials and ebooks about || C#, Java, Delphi, C, R, Swift, Python, Assembly, PL/SQL, Scratch, Go, Visual Basic, C++, Ruby, PHP, Visual Basic .NET, JavaScript, Objective-C, Perl, MATLAB Hack, Fortran, Alice, Kotlin, Apex, Clojure, LabVIEW, Rust, D, VBScript, ML, Prolog, Crystal, Julia, Erlang, Logo, Scheme, FoxPro, Lisp, F#, Bash, Scala, ABAP, Dart, Lua, Transact-SQL, COBOL, Ada, Awk, SAS || ___

Sign In or Register to comment.