I've posted my C code below for a card game (yep, i'm still on it). Now the neg() function is called from the turn() function. so using return; sends it back there, but i want it to go back to the start of the main function (basicaly start again). As with in the eval function i want it to go back to the beginning of the main function if the input is y.

Also when n is the input, i want the program to just print the line "ok see ya" then finish, but it keeps looping either to the turn function or the eval. :cry:
ignore the "path" bits and functions for now. that's for when images are used in java..

#include <stdio.h>
#include<time.h>
 
/*functions i'm going to use*/
void turn(int total, int other);
int draw(int total, int other);
int neg(int total, int other);
int eval(int total, int other);
/*variable for path*/
int path;
int main()
{
/*variables for cards*/
int card3;
int total;
int other;
/*intializing other with the random function*/
srand(time(NULL));
other=(rand()%22)+1;
/*letters of suit, plus random to choose which, then variables to store suit*/
char type[4]={'S','D','H','C'};
int t=(rand()%3)+1;
int u=(rand()%3)+1;
char suit=type[t];
char suit2=type[u];
/*values of cards and random to choose one, then variables to store card value*/
int set[8]={2,3,4,5,6,7,8,9};
int i=(rand()%7)+1;
int j=(rand()%7)+1;
int card=set[i];
int card1=set[j];
/*total of two cards*/
total = card+card1;
printf("Hey, welcome to HBK's Twist or bust game. Lets begin\n");
/*To display first card, using switch to match suit then go to each function to 
find "path" of image, for when images used*/
switch(suit)
 {
  case 'S' : printf("First card is %d of Spades", card); spade(i); 
break;
  case 'D' : printf("First card is %d of Diamonds", card);
 diamond(i); break;
  case 'H' : printf("First card is %d of Hearts", card);
 heart(i); break;
  case 'C' : printf("First card is %d of Clubs", card);
 heart(i); break;
 }
/*show path value, just to see if it worked really*/
printf("path is %d\n", path);
/*switch for second card, same as above*/
switch(suit2)
 {
  case 'S' : printf("\tSecond card is %d of Spades\n", card1); 
spade(j); break;
  case 'D' : printf("\tSecond card is %d of Diamonds\n", card1);
 diamond(j); break;
  case 'H' : printf("\tSecond card is %d of Hearts\n", card1);
 heart(j); break;
  case 'C' : printf("\tSecond card is %d of Clubs\n", card1);
 heart(j); break;
 }
/*show total*/
 printf("The total is: %d\n", total);
/*show path value, just to see if it worked really*/
printf("path is %d\n", path);
 
/*go to eval function*/
 eval(total, other);
fputs("after eval", stdout);
return 0;
}
/*4 functions which will be used to find image path, in java game if that will 
work*/
int spade(int p)
{
 int set[8]={12,13,14,15,16,17,18,19};
path=set[p];
 
return path;
}
int diamond(int p)
{
 int set[8]={12,13,14,15,16,17,18,19};
path=set[p];
 
return path;
}
int heart(int p)
{
 int set[8]={12,13,14,15,16,17,18,19};
path=set[p];
 
return path;
}
int club(int p)
{
 int set[8]={12,13,14,15,16,17,18,19};
path=set[p];
 
return path;
}
/*eval function which looks at the total made in main and decides where to go*/
int eval(int total, int other)
{ char again;
if (total==21)
 {
 printf("Waho! WINNER!!!!!!!!\n Play again?");
/*get response, clear newline as well*/
 again = getchar();
while (getchar() != '\n');
/*(if they say yes, go back to main, if no exit*/
 switch(again)
 { case 'y' : return; break;
  case 'n' : printf("ok, see ya!\n"); break;
 }
 }
/*if total less than 21 go to turn function*/
 while(total<21)
 { turn(total, other); }
 
/*If total bigger than 21 offer to play again*/
 if (total>21);
 { printf("BUST! play again?\n"); 
/*get response, clear newline as well*/
 again = getchar();
while (getchar() != '\n');
/*(if they say yes, go back to main, if no exit*/
 switch(again)
 { case 'y' : return; break;
  case 'n' : printf("ok, see ya!\n"); break;
 }
}
}
/*turn function*/
void turn(int total, int other)
{ char ans;
/*get response, clearing newline*/
 printf("Twist or Stick? (t/s)\n");
 ans = getchar();
while (getchar() != '\n');
/*if "twist" (draw another card) go to draw function, if "stick" go to neg 
function*/
 switch(ans) 
 { case 't' : draw(total, other); break;
  case 's' : neg(total, other); break;
 }
}
/*draw function*/
int draw(int total, int other)
{
/*letters of suit, plus random to choose which then variables to store suit*/
char type[4]={'S','D','H','C'};
int t=(rand()%3)+1;
char suit=type[t];
/*values of cards and random to choose one, then variables to store card value*/
int set[8]={2,3,4,5,6,7,8,9};
int i=(rand()%7)+1;
int card=set[i];
/*switch block to decide which suit to show as in main*/
switch(suit)
 {
  case 'S' : printf("Next card is %d of Spades", card); spade(i); 
break;
  case 'D' : printf("Next card is %d of Diamonds", card);
 diamond(i); break;
  case 'H' : printf("Next card is %d of Hearts", card);
 heart(i); break;
  case 'C' : printf("Next card is %d of Clubs", card);
 heart(i); break;
 }
 
/*total variable from above now equales total + card three*/
  total+=card; printf("\nNew total: %d\n", total);
/*go back to the "eval" function with the new total 
*/
eval(total, other);
}
 
/*neg function*/
int neg(int total, int other)
{ char win, lose;
 
/*if total is bigger than the other in the main function then say you won etc*/
 if(total > other)
 { printf("Opponents cards: %d\n", other);
  printf("you won! play again?\n");
 
win = getchar();
while (getchar() != '\n');
 
/*if the answer is yes, go back to main, if no finish. except it returns to the 
turn function*/
switch(win)
 { case 'y' : return; break;
  case 'n' : printf("ok, see ya!\n"); break;
 }
 }
 
/*if other in main is bigger than total say you lose etc*/
 
 else if (other > total)
 {printf("Opponents cards: %d\n", other);
  printf("damn nice try. play again?\n"); 
lose = getchar();
while (getchar() != '\n');
 
/*if the answer is yes, go back to main, if no finish*/
switch(lose)
 { case 'y' : return main; break;
  case 'n' : printf("ok, see ya!\n"); break;
 }
 
 }
 
}
Comments
Nice start (thwee)

The best way to approach this is to create a new program where you can practise getting the thing to loop back to the beginning.

Flow charts also help.

You also need to take care of the way you write your code. Your braces "{" need to be aligned.

Use a while loop around the game code, and keep looping through it until the game is over. This essentially will allow you to "go back" to the beginning of the main funciton, only where you need it to go though.

I've attached a pretty little flow chart (badly drawn i know) showing where i want stuff to go.

Isn't it already in a while loop? as while total < 21 it goes to turn function, which sends it to draw a card and back to eval or to the neg. The neg function is the problem, as it reads the users choice, and then even if n is entered, it returns to the turn function:

Hey welcome to HBK's Twist or Bust game. let's begin
first card is 4 of hearts
Second card is 8 of diamonds
total is: 12
Twist or stick? (t\s)
s
Opponents cards: 2
you won! play again?
y
Twist or stick? (t\s)
s
Opponents cards: 2
you won! play again?
n
ok, see ya!
Twist or stick? (t\s)

#include <stdio.h>
#include<time.h>
 
/*functions i'm going to use*/
void turn(int total, int other);
int draw(int total, int other);
int neg(int total, int other);
int eval(int total, int other);
 
int main()
{
/*variables for cards*/
 int card3;
 int total;
 int other;
 
/*intializing other with the random function*/
 srand(time(NULL));
 other=(rand()%22)+1;
 
/*letters of suit, plus random to choose which, then variables to store suit*/
 char type[4]={'S','D','H','C'};
 int t=(rand()%3)+1;
 int u=(rand()%3)+1;
 char suit=type[t];
 char suit2=type[u];
 
/*values of cards and random to choose one, then variables to store card value*/
 int set[8]={2,3,4,5,6,7,8,9};
 int i=(rand()%7)+1;
 int j=(rand()%7)+1;
 int card=set[i];
 int card1=set[j];
 
/*total of two cards*/
 total = card+card1;
 
 printf("Hey, welcome to HBK's Twist or bust game. Lets begin\n");
 
/*To display first card, using switch to match suit */
 
 switch(suit)
 {
  case 'S' : printf("First card is %d of Spades", card); break;
  case 'D' : printf("First card is %d of Diamonds", card); break;
  case 'H' : printf("First card is %d of Hearts", card); break;
  case 'C' : printf("First card is %d of Clubs", card); break;
 }
 
/*switch for second card, same as above*/
 switch(suit2)
 {
  case 'S' : printf("\tSecond card is %d of Spades\n", card1); break;
  case 'D' : printf("\tSecond card is %d of Diamonds\n", card1); break;
  case 'H' : printf("\tSecond card is %d of Hearts\n", card1); break;
  case 'C' : printf("\tSecond card is %d of Clubs\n", card1); break;
 }
 
/*show total*/
 printf("The total is: %d\n", total);
 
 
/*go to eval function*/
 eval(total, other);
 
 fputs("after eval", stdout);
 return 0;
}
 
/*eval function which looks at the total made in main and decides where to go*/
 
int eval(int total, int other)
{ 
 char again;
 
 if (total==21)
     {
          printf("Waho! WINNER!!!!!!!!\n Play again?");
 
           /*get response, clear newline as well*/
          again = getchar();
         while (getchar() != '\n');
 
         /*(if they say yes, go back to main, if no exit*/
           switch(again)
                { 
                       case 'y' : return; break;
                       case 'n' : printf("ok, see ya!\n"); break;
                }
    }
 
/*if total less than 21 go to turn function*/
 
 while(total<21)
    { 
         turn(total, other); 
     }
 
/*If total bigger than 21 offer to play again*/
 
 while (total>21);
     { 
         printf("BUST! play again?\n"); 
/*get response, clear newline as well*/
          again = getchar();
         while (getchar() != '\n');
 
/*(if they say yes, go back to main, if no exit*/
         switch(again)
               { 
                    case 'y' : return; break;
                    case 'n' : printf("ok, see ya!\n"); break; 
               }
      }
}
 
/*turn function*/
 
void turn(int total, int other)
{ 
     char ans;
 
    /*get response, clearing newline*/
     printf("Twist or Stick? (t/s)\n");
     ans = getchar();
     while (getchar() != '\n');
/*if "twist" (draw another card) go to draw function, if "stick" go to neg function*/
      switch(ans) 
            { 
                 case 't' : draw(total, other); break;
                 case 's' : neg(total, other); break;
             }
}
 
/*draw function*/
 
int draw(int total, int other)
{
/*letters of suit, plus random to choose which then variables to store suit*/
         char type[4]={'S','D','H','C'};
         int t=(rand()%3)+1;
         char suit=type[t];
/*values of cards and random to choose one, then variables to store card value*/
         int set[8]={2,3,4,5,6,7,8,9};
         int i=(rand()%7)+1;
         int card=set[i];
/*switch block to decide which suit to show as in main*/
        switch(suit)
             {
                 case 'S' : printf("Next card is %d of Spades", card); break;
                 case 'D' : printf("Next card is %d of Diamonds", card); break;
                 case 'H' : printf("Next card is %d of Hearts", card); break;
                 case 'C' : printf("Next card is %d of Clubs", card); break;
              }
 
/*total variable from above now equales total + card three*/
          total+=card; printf("\nNew total: %d\n", total);
 
/*this is where i want it to go back to the "eval" function with the new total, is that line right?*/
        eval(total, other);
}
 
/*neg function*/
 
int neg(int total, int other)
{ 
          char win, lose;
 
/*if total is bigger than the other in the main function then say you won etc*/
         if(total > other)
                { 
                        printf("Opponents cards: %d\n", other);
                        printf("you won! play again?\n");
 
                       win = getchar();
                       while (getchar() != '\n');
 
/*if the answer is yes, go back to main, if no finish. except it returns to the turn function*/
                       switch(win)
                              { 
                                     case 'y' : return; break;
                                     case 'n' : printf("ok, see ya!\n"); break;
                               }
                  }
 
/*if other in main is bigger than total say you lose etc*/
 
          else if (other > total)
                 {
                       printf("Opponents cards: %d\n", other);
                        printf("damn nice try. play again?\n"); 
                        lose = getchar();
                         while (getchar() != '\n');
 
/*if the answer is yes, go back to main, if no finish*/
                   switch(lose)
                            { 
                                case 'y' : return; break;
                                case 'n' : printf("ok, see ya!\n"); break;
                             }
                  }
 
}

there's gotta be an easier way of formating that than using the space bar. In notepad (tabbed) it looks beautiful, copying it over here it goes all to the left and tab moves me out of the reply box..

Attachments flow.JPG 54.9 KB

Never use tabs, spaces are universal. Normally I would suggest three spaces for each indentation.

Well done on doing that flow chart! You are adopting good habits which will help you later. However, it is somewhat lacking a proper layout.

Flow charts should be read from top to bottom. I'll post an example later.

Hi there, I've attached how the flow should look. Things to notice. Each decision block has two links that lead to another option or the end.

In yours some of your decision blocks had only one arrow comming out of it. Notice how it reads from top to bottom.

Keep going!

Attachments kim.jpg 101.78 KB

Never use tabs, spaces are universal. Normally I would suggest three spaces for each indentation.

I agree, but if you use a program such as VS2005, the auto-indention will mess your formatting up. This option can be turned off in the options screen btw.

Edited 6 Years Ago by Nick Evan: n/a

Or use a smart IDE which will interpolate TAB with spaces...That way you can use tabbing in your program and be rest assured that the formatting won't be messed up since that tab would actually be spaces.

PS: Long time no see Niek...;)

Or you don't need to do any indenting and write as scruffily as possible. Then just use this:-

http://sourceforge.net/projects/astyle/
:-)

That's teaching new programmers bad habits. But since I never take your advice serious....
Just kidding offcourse :)

@~s.o.s.~. Good to be back!

Edited 6 Years Ago by Nick Evan: n/a

Applying that on kimmie's code, instant beauty...

#include <stdio.h>
#include <time.h>

/*functions i'm going to use*/
void turn( int total, int other );
int draw( int total, int other );
int neg( int total, int other );
int eval( int total, int other );

int main()
{
    /*variables for cards*/
    int card3;
    int total;
    int other;

    /*intializing other with the random function*/
    srand( time( NULL ) );
    other=( rand()%22 )+1;

    /*letters of suit, plus random to choose which, then variables to store suit*/
    char type[4]={'S','D','H','C'};
    int t=( rand()%3 )+1;
    int u=( rand()%3 )+1;
    char suit=type[t];
    char suit2=type[u];

    /*values of cards and random to choose one, then variables to store card value*/
    int set[8]={2,3,4,5,6,7,8,9};
    int i=( rand()%7 )+1;
    int j=( rand()%7 )+1;
    int card=set[i];
    int card1=set[j];

    /*total of two cards*/
    total = card+card1;

    printf( "Hey, welcome to HBK's Twist or bust game. Lets begin\n" );

    /*To display first card, using switch to match suit */

    switch ( suit )
    {
    case 'S' :
        printf( "First card is %d of Spades", card ); break;
    case 'D' :
        printf( "First card is %d of Diamonds", card ); break;
    case 'H' :
        printf( "First card is %d of Hearts", card ); break;
    case 'C' :
        printf( "First card is %d of Clubs", card ); break;
    }

    /*switch for second card, same as above*/
    switch ( suit2 )
    {
    case 'S' :
        printf( "\tSecond card is %d of Spades\n", card1 ); break;
    case 'D' :
        printf( "\tSecond card is %d of Diamonds\n", card1 ); break;
    case 'H' :
        printf( "\tSecond card is %d of Hearts\n", card1 ); break;
    case 'C' :
        printf( "\tSecond card is %d of Clubs\n", card1 ); break;
    }

    /*show total*/
    printf( "The total is: %d\n", total );


    /*go to eval function*/
    eval( total, other );

    fputs( "after eval", stdout );
    return 0;
}

/*eval function which looks at the total made in main and decides where to go*/

int eval( int total, int other )
{
    char again;

    if ( total==21 )
    {
        printf( "Waho! WINNER!!!!!!!!\n Play again?" );

        /*get response, clear newline as well*/
        again = getchar();
        while ( getchar() != '\n' );

        /*(if they say yes, go back to main, if no exit*/
        switch ( again )
        {
        case 'y' :
            return; break;
        case 'n' :
            printf( "ok, see ya!\n" ); break;
        }
    }

    /*if total less than 21 go to turn function*/

    while ( total<21 )
    {
        turn( total, other );
    }

    /*If total bigger than 21 offer to play again*/

    while ( total>21 );
    {
        printf( "BUST! play again?\n" );
        /*get response, clear newline as well*/
        again = getchar();
        while ( getchar() != '\n' );

        /*(if they say yes, go back to main, if no exit*/
        switch ( again )
        {
        case 'y' :
            return; break;
        case 'n' :
            printf( "ok, see ya!\n" ); break;
        }
    }
}

/*turn function*/

void turn( int total, int other )
{
    char ans;

    /*get response, clearing newline*/
    printf( "Twist or Stick? (t/s)\n" );
    ans = getchar();
    while ( getchar() != '\n' );
    /*if "twist" (draw another card) go to draw function, if "stick" go to neg function*/
    switch ( ans )
    {
    case 't' :
        draw( total, other ); break;
    case 's' :
        neg( total, other ); break;
    }
}

/*draw function*/

int draw( int total, int other )
{
    /*letters of suit, plus random to choose which then variables to store suit*/
    char type[4]={'S','D','H','C'};
    int t=( rand()%3 )+1;
    char suit=type[t];
    /*values of cards and random to choose one, then variables to store card value*/
    int set[8]={2,3,4,5,6,7,8,9};
    int i=( rand()%7 )+1;
    int card=set[i];
    /*switch block to decide which suit to show as in main*/
    switch ( suit )
    {
    case 'S' :
        printf( "Next card is %d of Spades", card ); break;
    case 'D' :
        printf( "Next card is %d of Diamonds", card ); break;
    case 'H' :
        printf( "Next card is %d of Hearts", card ); break;
    case 'C' :
        printf( "Next card is %d of Clubs", card ); break;
    }

    /*total variable from above now equales total + card three*/
    total+=card; printf( "\nNew total: %d\n", total );

    /*this is where i want it to go back to the "eval" function with the new total, is that line right?*/
    eval( total, other );
}

/*neg function*/

int neg( int total, int other )
{
    char win, lose;

    /*if total is bigger than the other in the main function then say you won etc*/
    if ( total > other )
    {
        printf( "Opponents cards: %d\n", other );
        printf( "you won! play again?\n" );

        win = getchar();
        while ( getchar() != '\n' );

        /*if the answer is yes, go back to main, if no finish. except it returns to the turn function*/
        switch ( win )
        {
        case 'y' :
            return; break;
        case 'n' :
            printf( "ok, see ya!\n" ); break;
        }
    }

    /*if other in main is bigger than total say you lose etc*/

    else if ( other > total )
    {
        printf( "Opponents cards: %d\n", other );
        printf( "damn nice try. play again?\n" );
        lose = getchar();
        while ( getchar() != '\n' );

        /*if the answer is yes, go back to main, if no finish*/
        switch ( lose )
        {
        case 'y' :
            return; break;
        case 'n' :
            printf( "ok, see ya!\n" ); break;
        }
    }

}

Yes astyle is good, but good IDE's always come with the same feature inbuilt. Take for example Code::Blocks and Eclipse. They both come with the feature of automatic indentation of code.

wow. so beautiful *wipes tear* ;)

I tried the flow chart portrait but ran out of room in the word document, but thanks.. your's does look better :)

for Java i use NetBeans so that's all formatted. But C is all Notepad (which no sells the formatting >:( ) Spaces it is.

So now it's checking against the flow, and working out where to put the loop so i can return to main and break out when i want to

Yes astyle is good, but good IDE's always come with the same feature inbuilt. Take for example Code::Blocks and Eclipse. They both come with the feature of automatic indentation of code.

And good editors, too :mrgreen:

This article has been dead for over six months. Start a new discussion instead.