Hi everyone. Could someone please help me with this. I have a program which i have written and finished for a college assignment. It is completely done. I have a perfect working copy on my laptop but when i copy it to usb stick and take it to college it decides not to work. Its driving me crazy because the code works!! It happens at the part when it asks wether you want a return or not. The do while loop should work fine and it does at home. But when transfered to colege it just freezes at that point and whatever you input, letter or number, it just keeps looping. Its so anoying as i have it working. I know it should probably be %c instead of %s but for some reason it works that way and i have tried changing this among other things and it still doesn't work.
If anyone can help it would be really apreciated.
Thanks everyone :)

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



int main(int argc, char *argv[])
{
    
  int y, n, leaving, arriving, ret, check, price;                                     
  
  
  
do {                                                                                  //Do while loop for whole program          


         do{                                                                         //Do while loop for leaving station choice
  printf("\n\n ##########  Welcome to the ticket machine  ########## \n\n");         //Welcome message 
          
  printf("\n  Please select a leaving station from the list below\n");                   
  printf("  by entering the corresponding number :\n");
  printf("\n  1 = Ashford \n  2 = Brentworth \n  3 = Canonbury Cross \n  4 = Dowgate \n  5 = Edbury \n");
  printf("  6 = Fenchurch Street \n  7 = Gresham \n  8 = Hampstead \n  9 = Islington \n  10 = Jamaica Road \n");
  scanf("%d", &leaving);                                                                 
  system ("CLS");
         } while (leaving <1 || leaving >10);
  
       
         do {                                                                        //Do while loop for arriving station choice
  printf("\n\n ##########  Welcome to the ticket machine  ########## \n\n");      
  printf("\n  Please select an arriving station from the list below\n ");  
  printf(" by entering the corresponding number :\n");
  printf("\n  1 = Ashford \n  2 = Brentworth \n  3 = Canonbury Cross \n  4 = Dowgate \n  5 = Edbury \n");
  printf("  6 = Fenchurch Street \n  7 = Gresham \n  8 = Hampstead \n  9 = Islington \n  10 = Jamaica Road \n");
  scanf("%d", &arriving);      
  system ("CLS");
         } while (arriving <1 || arriving >10);
 
  
  
  do                                                                         //Do while loop for return ticket choice
         {
         system("CLS");
         printf("\n\n ##########  Welcome to the ticket machine  ########## \n\n\n");
         printf("\n Do you require a return ticket?");
         printf("\n\n Press y for a return ticket.\n");
         printf(" Press n if a return ticket is not required:\n");
         scanf("%s", &ret);                                                 //Assign return choice to &ret
         }    
  while (ret != 'y' && ret != 'n');                                         //Keep looping if y or n are not selected
  
  
  
  do {                                                                      //Do while loop for ticket summary
  system ("CLS");                                                           //Clear screen for ticket summary
  
  
  
  //TICKET SUMMARY//
  
  
  printf("\n             #####  TICKET SUMMARY  #####\n\n\n");              //Ticket summary message
  
  
   switch (leaving)
   {
                 case 1:
                      printf("\n You have selected Ashford ");
                      break;
                 case 2: 
                      printf("\n You have selected Brentworth ");
                      break;
                 case 3:
                      printf("\n You have selected Canonbury Cross ");
                      break;
                 case 4:
                      printf("\n You have selected Dowgate ");
                      break;
                 case 5:
                      printf("\n You have selected Edbury ");
                      break;
                 case 6:
                      printf("\n You have selected Fenchurch Street ");
                      break;
                 case 7:
                      printf("\n You have selected Gresham ");
                      break;
                 case 8:
                      printf("\n You have selected Hampstead ");
                      break;
                 case 9:
                      printf("\n You have selected Islington ");
                      break;
                 case 10:
                      printf("\n You have selected Jamaica Road ");
                      break;   
   }
          
  printf("as your leaving station.\n\n");
  
  switch (arriving)
  {
                 case 1:
                      printf("\n You have selected Ashford ");
                      break;
                 case 2: 
                      printf("\n You have selected Brentworth ");
                      break;
                 case 3:
                      printf("\n You have selected Canonbury Cross ");
                      break;
                 case 4:
                      printf("\n You have selected Dowgate ");
                      break;
                 case 5:
                      printf("\n You have selected Edbury ");
                      break;
                 case 6:
                      printf("\n You have selected Fenchurch Street ");
                      break;
                 case 7:
                      printf("\n You have selected Gresham ");
                      break;
                 case 8:
                      printf("\n You have selected Hampstead ");
                      break;
                 case 9:
                      printf("\n You have selected Islington ");
                      break;
                 case 10:
                      printf("\n You have selected Jamaica Road ");
                      break;   
  }
          
  printf("as your arriving station.\n\n");
  

  
     if (ret == 'y')                                                                 //Print summary of return choice
          {
          printf("\n You have selected a return ticket\n\n");                        //Show if return has been chosen
          }
  else if (ret == 'n')
          {
          printf("\n You have not selected a return ticket\n\n");                    //Show if return has not been chosen
          }
  
  //END OF TICKET SUMMARY
  
 
  printf("\n\n\n     Are these the correct options for your ticket?\n\n");           //Show summary of choices made
  printf("     Press y if they are correct.\n     Press n if you want to make your selections again:\n");     
  scanf("%s", &check);
  
  if (check != 'y' && check != 'n')
            {
            system ("CLS");                                                         //Clear screen if not selected y or n
            }

  if (check == 'y')
            {
            system ("CLS");
            printf("\n\n  Your selections have been saved.\n\n");                   //Show if choices are confirmed
            }
            
   else if (check == 'n')
            {
            system ("CLS");
            printf("\n\n  Your selections have been cleared.\n  Please make your choices again.\n\n"); 
            }
        
} while (check != 'y' && check != 'n');             //End of loop for ticket summary
        
} while (check != 'y');                             //End of loop for whole choices selection program
  
  
  //Pricing part
   
  if (leaving > arriving)                           //If leaving greater than arriving then subtract arriving
              {
              price = leaving*2 - arriving*2;  
              }
  
  else
              {                                      //Else subtract leaving from arriving
              price = arriving*2 - leaving*2;  
              }
  
  if (ret == 'y')                                    //If return was picked times ticket by 1 and half
  {price = price*1.5;}
  
  
  printf("\n\n  Your ticket price is : ");           //Show ticket price
  printf(" %c %d \n\n", 156, price);
  printf("  Please make payment and collect ticket.\n\n");
  
  
  
  
  system("PAUSE");	
  return 0;
}

Just to clarify its the do while loop between lines 41 and 50 that stops working. I put the whole program in so you can see what i have. Thanks :)

main.cpp:48: warning: format '%s' expects type 'char*', but argument 2 has type 'int*'
main.cpp:153: warning: format '%s' expects type 'char*', but argument 2 has type 'int*'

You want to treat these warnings as actual errors and fix them.

PS. The line numbers of these two warnings match the line numbers in the code you've posted.

can you tell me if it is asking for the the choice for the return ticket or is it looping again and again without asking you for return ticket......

If it is not asking you for a yes or no for return ticket and continuing with the loop then it is the problem of input stream not getting flushed ....... i can give you the code for it if that is the problem ...also please let me know of the enviroment your running your prog in

Hi
It asks for a choice of return ticket or not. You enter y and it just clears and asks for return o not again. And whatever you input it just stays on that part asking for a choice of return or not.
I understand that it should be %c for the choice as it is a letter they are entering. But i have just realised that if i change it to %c it does exactly what it does when i transfer it to the college computer. If you change it and run the program you will see what i mean.
It seems very strange to me as i have other do while loops in the program that are virtually the same that work fine. Very odd.
Thanks for all your help :)

oh also, i'm just running it in dev C++ if that helps. My knowledge of programming is very limited as this is the first i have ever done. Sorry if my descriptions are a little off.

Oh hang on. Iv'e just had another look. What i've done is changed my declerations at the top. I've changed the check and the ret and declared them as char instead of int. It seems to work like this. I'm still not sure if i will get it to college and it will play up again but i would have thought, that would sort it out. Declaring them as int's is going to cause the problem i would have thought. Anyone think this is right??

Also ...there is one more problem ..... when u accept a string you dont use & with variable .....it is used for char and int variables only and not for strings...u have declared your ret as string ...change it to char and try

Fantastic. I've changed it now and it all looks good. Really satisfying when you get it sorted. Thanks for all the help. Very much appreciated :)

It's essential that you get these format strings right from the very beginning. Since you mentioned Dev-C++, I take that your compiler is gcc or g++. If so, then pass the -Wall compiler flag to your compiler - that enables most of the warnings and should make it detect mismatching arguments to printf/scanf et al.

Some related documentation -> fscanf/scanf/sscanf

The other essential thing is to NEVER use scanf() to read a string. It's the same as using gets() -- see this series.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.