0

I wanted to make a program that asks the user questions and then the user selects an answer and then the program returns back to the original directory of questions. for example:

What's your favorite color? 1. Blue 2. Green 3. Yellow

then the user selects say "2"

then I want it to return back to

What's your favorite color? 1. Blue 2. Green 3. Yellow

I have the code worked out to handle asking, selecting answers to the questions. I just need help returning back to the original question so the user can answer it again if they would like. thanks people!

#include <stdio.h>

int main (void)
{
   printf("\nIntroducing Space\n"); 
   printf("Brought to you in part by Free Time\n\n\n");
   while(1) {   
       int space;
       int subspace1;
       int subspace2;
       printf("What would you like to do in Space?\n");
       printf("\nDIRECTORY\n\n");
       printf("1. What is space?\n");
       printf("2. Tell me how cool I am\n");
       printf("Enter the number to your desired space\n");
       scanf("%i", &space);
       if (space == 2){
           printf("Although I have never met you, anyone in space would be considered    cool to the average eye. I mean lets be real, its space.\n\n");
           printf("Do you have any interesting hobbies or tidbits about yourself?\n\n");
           printf("1. I can play the flute blindfolded\n");
           printf("2. I can eat twelve pounds of salt water taffy in one sitting\n");
           printf("3. My uncle drives an RV\n");
           scanf("%i", &subspace2);
           if (subspace2 == 1) {
              printf("\n The flute huh? You're somethin' else\n");
           }
           if (subspace2 == 2) {
              printf("\n But let's be honest who likes salt water taffy these days anyway?\n");
           }
           if (subspace2 == 3) {
              printf("\n An RV? Now adays they call that 'homeless' but hey whatever floats your boat chief\n");
           }
      }
   }
   return 0;
}

I feel like this is where I need some sort of return statement that brings me back to my original question "What would you like to do in space" so the user can ask another question.

Jekyll
  • 1,434
  • 11
  • 13
user2990129
  • 67
  • 1
  • 6

5 Answers5

1

From your question, it is not clear what actually you are trying to achieve, but i think a simple loop will do.

check this code

#include <stdio.h>
#include <stdlib.h>

int main (void)
{


    printf("\nIntroducing Space\n");                                
    printf("Brought to you in part by Free Time\n\n\n");


    int space;
    int subspace1;
    int subspace2;

    while (1)
    {
        printf("What would you like to do in Space?\n");                
        printf("\nDIRECTORY\n\n");
        printf("1. What is space?\n");
        printf("2. Tell me how cool I am\n");
        printf("3. no more question. I want to get out\n");


        printf("Enter the number to your desired space\n");
        scanf("%d", &space);

        if (space == 0)
            exit(0);
        else if (space ==1)
        {
            printf("First go to wikipedia and read about the space\n\n");
        }
        else if (space == 2){
            printf("Although I have never met you, anyone in space would be considered    cool to the average eye. I mean lets be real, its space.\n\n");
            printf("Do you have any interesting hobbies or tidbits about yourself?\n\n");
            printf("1. I can play the flute blindfolded\n");
            printf("2. I can eat twelve pounds of salt water taffy in one sitting\n");
            printf("3. My uncle drives an RV\n");
            scanf("%i", &subspace2);
            if (subspace2 == 1) {
                printf("\n The flute huh? You're somethin' else\n");
            }
            if (subspace2 == 2) {
                printf("\n But let's be honest who likes salt water taffy these days anyway?\n");
            }
            if (subspace2 == 3) {
                printf("\n An RV? Now adays they call that 'homeless' but hey whatever floats your boat chief\n");
            }
        }
        else
            printf("Please enter the choice properly\n\n");
    }
    return 0;
}

However, when the choice number will grow large, its better to use switch. Give it a try. Happy coding!!

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
1

Pseudocode:

while(1) {
    //ask question 
    if (answer == 'q') //quit
       //exit from the loop, e.g.: break or return
    //....
}
Karoly Horvath
  • 94,607
  • 11
  • 117
  • 176
1

You should use a loop. You should also have a look at the switch statement. In the long term it would probably better if you'd use a good book for learning C.

Example:

#include <stdio.h>
int main() {
    char c;
    while(scanf("%c", &c) > 0) {
        switch(c) {
        case 'q':
        case 'Q':
            /* exit loop */
            break;
        default:
            /* do something */
            printf("%c", c);
        }
    }
}
Community
  • 1
  • 1
moooeeeep
  • 31,622
  • 22
  • 98
  • 187
0

Loop apart the way in which you use the if statement needs to be commented, you write:

scanf("%i", &subspace2);
if (subspace2 == 1) {
    printf("\n The flute huh? You're somethin' else\n");
}
if (subspace2 == 2) {
    printf("\n But let's be honest who likes salt water taffy these days anyway?\n");
}
if (subspace2 == 3) {
    printf("\n An RV? Now adays they call that 'homeless' but hey whatever floats your boat chief\n");
}

The previous code tests the subspace2 variable three times. Instead of using that you should use:

scanf("%i", &subspace2);
if (subspace2 == 1) {
    printf("\n The flute huh? You're somethin' else\n");
}
else if (subspace2 == 2) {
    printf("\n But let's be honest who likes salt water taffy these days anyway?\n");
}
else if (subspace2 == 3) {
    printf("\n An RV? Now adays they call that 'homeless' but hey whatever floats your boat chief\n");
}

Which evaluates the second branch only if the first one is not true and the third one only if the first and the second are not true, but the best option will probably be:

scanf("%i", &subspace2);
switch(subspace2) {
    case 1:
        printf("\n The flute huh? You're somethin' else\n");
        break;
    case 2:
        printf("\n But let's be honest who likes salt water taffy these days anyway?\n");
        break;
    case 3:
        printf("\n An RV? Now adays they call that 'homeless' but hey whatever floats your boat chief\n");
        break;
    default:
        printf("\n invalid option\n" );
}

which will be implemented by a jump table that it will be faster.

Jekyll
  • 1,434
  • 11
  • 13
0

Use do...while instead of while since you mention you ask the question at least once. This is how you can do it

int choice;
char ch2;
do{

printf("Option1");
printf("Option2");
printf("Option3");
scanf("%d", choice);

switch(choice){
case 1: //statements
        break;
case 2: //statements
        break;
case 3: //statements
        break;
deafult: //statements
}

printf("Do you want to continue(c) or quit(q)?");
scanf("%s", ch2)
}while(ch2 != "q");
Kevin
  • 566
  • 2
  • 5
  • 13