4 Replies - 297 Views - Last Post: 19 February 2013 - 09:48 AM Rate Topic: -----

#1 Coolai21  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 07-August 12

Program stops working after this integer input

Posted 19 February 2013 - 08:16 AM

Hello again dic users~
So I just started programming in C and so far so good.
I wanted to make a sample menu program that uses switch and loops for the logic.
Now the problem:
When I want to go back to the menu program I'll need to input a 1 for yes or a 0 for no.
After that input the program stops and I don't know why.

The code:
#include<stdio.h>
#include <stdlib.h>

// start main
int main() {
    
    int option, response;
    
    int optOne(), optTwo(), optThree();
    
    do {
        
        printf("Menu:\nPress 1 for Option One.\nPress 2 for Option Two.\nPress 3 for Option Three.\n");
        scanf("%d", &option);
        
        switch(option)
        {
                      case 1:
                      system("cls");
                      optOne();
                      /*!!!-------------After this input my program stops working--------------------!!!*/
                      printf("Do you want to go back to main menu?[1 for YES/0 for NO]\n");
                      scanf("%d", &response);
                      system("cls");
                      break;
                      
                      case 2:
                      system("cls");
                      optTwo();
                      /*!!!-------------After this input my program stops working--------------------!!!*/
                      printf("Do you want to go back to main menu?[1 for YES/0 for NO]\n");
                      scanf("%d", &response);
                      system("cls");
                      break;
                      
                      case 3:
                      system("cls");
                      optThree();
                      /*!!!-------------After this input my program stops working--------------------!!!*/
                      printf("Do you want to go back to main menu?[1 for YES/0 for NO]\n");
                      scanf("%d", &response);
                      system("cls");
                      break;              
        }
        
        
        
        } while(response == 1);
    
       
} // end of main

void optOne() {
     
     char response;
     int counter = 1;
     // start of a while loop
     while(counter != 0) {
                   
                   printf("This is Option One.\n");
                   printf("Do you want to try again:[Y/N]\n");
                   scanf("%s", &response);
                   
                   if(response == 'y' || response == 'Y')
                    {
                       continue;            
                    }
                    else if(response == 'n' || response == 'N')
                    {
                       system("cls");
                       counter = 0;
                    }
                    else
                    {
                        printf("Invalid input\n");
                    }
                   
                   }
     
     } // end of optOne function
     
void optTwo() {
     
     char response;
     int counter = 1;
     // start of a while loop
     while(counter != 0) {
                   
                   printf("This is Option Two.\n");
                   printf("Do you want to try again:[Y/N]\n");
                   scanf("%s", &response);
                   
                   if(response == 'y' || response == 'Y')
                    {
                       continue;            
                    }
                    else if(response == 'n' || response == 'N')
                    {
                       system("cls");
                       counter = 0;
                    }
                    else
                    {
                        printf("Invalid input\n");
                    }
                   
                   }
     
     } // end of optTwo function
     
void optThree() {
     
     char response;
     int counter = 1;
     // start of a while loop
     while(counter != 0) {
                   
                   printf("This is Option Three.\n");
                   printf("Do you want to try again:[Y/N]\n");
                   scanf("%s", &response);
                   
                   if(response == 'y' || response == 'Y')
                    {
                       continue;            
                    }
                    else if(response == 'n' || response == 'N')
                    {
                       system("cls");
                       counter = 0;
                    }
                    else
                    {
                        printf("Invalid input\n");
                    }
                   
                   }
     
     } // end of optThree function




Thanks for the time reading it :]

Is This A Good Question/Topic? 0
  • +

Replies To: Program stops working after this integer input

#2 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3652
  • View blog
  • Posts: 11,421
  • Joined: 05-May 12

Re: Program stops working after this integer input

Posted 19 February 2013 - 08:59 AM

You are overrunning your buffer on line 91. You are asking the system to input a C-string which is will null terminate the input, but you only give room for a single character.
Was This Post Helpful? 1
  • +
  • -

#3 Coolai21  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 07-August 12

Re: Program stops working after this integer input

Posted 19 February 2013 - 09:31 AM

Thanks it was fixed (changed %s to %c) but another bug showed up.

Now it outputs the line "Invalid input" when I select one of the response (for example '1') and it also does the same when I try again.

It seems like after I entered response a in the
printf("Do you want to try again:[Y/N]\n");
line it immediately assumes that I inputted something or I skipped the input.

What is causing that?
Was This Post Helpful? 0
  • +
  • -

#4 simeesta  Icon User is offline

  • Deadly Ninja


Reputation: 219
  • View blog
  • Posts: 592
  • Joined: 04-August 09

Re: Program stops working after this integer input

Posted 19 February 2013 - 09:39 AM

The newline is being read, instead of your character. To remove it from the input stream you could use
getchar();

This post has been edited by simeesta: 19 February 2013 - 09:40 AM

Was This Post Helpful? 1
  • +
  • -

#5 Coolai21  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 07-August 12

Re: Program stops working after this integer input

Posted 19 February 2013 - 09:48 AM

Thanks! I got it...

I could use
system("cls");
that I used already but the bug is still in there and your advice worked perfectly!

I put
getchar();
on top of
printf("This is Option One.\n");


Thanks for all the help Skydiver & simeesta!
Hope I continue this simple program without bugs appearing again! :gunsmilie:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1