Hi. I need some help with some code i'm writing. I'm new to c++ and to be honest i feel like i have tried everything but cant get this bit working. I've included the small part of the program i have so far. I still have a lot to do but am stuck on this part. The user picks a choice of train station then picks another train station from the same list. I have succesfully set it up so a choice can be made and then the next choice but when i try to show the two choices in the line //Show summary of chosen station, i cannot get it to show the station names, only some symbols. I some how need to address the station names so they can be accessed throughout the program and i have tried lots of thingsbut cant get it working. If anyone can help it would be much appreciated.

int main(int argc, char *argv[])
{
    int leaving, arriving, choice1, choice2;
    char stationsswitch(int);

            
                                                     
           printf("\nEnter the station you will be leaving from by \nentering the corresponding number from the list below");
           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);                       //Put number in leaving
           choice1 = stationsswitch(leaving);             //Use function to pick station using leaving choice number

          printf(" as your leaving station\n");                //show chosen station
                  
          
          
          printf("\nEnter the station you will be arriving at by \nentering the corresponding number from the list below");
          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);                         //Put number in arriving
          choice2 = stationsswitch(arriving);            //Use function to pick station using arriving choice number
          
          printf(" as your arriving station.\n\n");                    //show chosen station
          
          
          printf("You have selected %c and %c as your stations\n\n", choice1, choice2);  //Show summary of chosen stations
          
          
  system("PAUSE");	
  return 0;
}


char stationsswitch(int choice1)           //Function for deciding station choice
{
     
     switch (choice1)
          {
                 case 1:
                      system ("CLS");
                      printf("You have selected Ashford ");
                      break;
                 case 2:
                      system ("CLS"); 
                      printf("You have selected Canonbury Cross ");
                      break;
                 case 3:
                      system ("CLS");
                      printf("\nYou have selected Canonbury Cross ");
                      break;
                 case 4:
                      system ("CLS");
                      printf("\nYou have selected Dowgate ");
                      break;
                 case 5:
                      system ("CLS");
                      printf("\nYou have selected Edbury ");
                      break;
                 case 6:
                      system ("CLS");
                      printf("\nYou have selected Fenchurch Street ");
                      break;
                 case 7:
                      system ("CLS");
                      printf("\nYou have selected Gresham ");
                      break;
                 case 8:
                      system ("CLS");
                      printf("\nYou have selected Hampstead ");
                      break;
                 case 9:
                      system ("CLS");
                      printf("\nYou have selected Islington ");
                      break;
                 case 10:
                      system ("CLS");
                      printf("\nYou have selected Jamaica Road ");
                      break;
                 
          }

}

You may have to re-think about your solution. Now, you are trying to print choice1 and choice2 as characters (%c). Then look at your definition of choice1 and choice2. They are integers values! So, what you do is that you translate an integer value to a character, then trying to print it to the screen.

I suggest you take a look at how to make strings (either the string class in <string>, or using char*). Some hints are: references and your stationsswitch funtion. Also consider changing its return type from char to int or bool (for error checking), or even void, as it really doesn't return anything at all at this point..

Good luck to you, and I hope I pointed you in the right direction in a clear enough way without spoiling too much ;)

Emil Olofsson

Edit: After taking another look at your code, I realize I interpreted what your code does sligthly different from what it really does. I see you want choice1 & 2 to contain the chosen station like choice1 = "Jamaica road", right? Still, there are several errors along the way of doing what you would like to to. First, your choice variables are integers, they should be strings (or char *'s). Second, your function is designed to return a char (not really compatible with integer), it should return a string (or a char *). Third, your function does not return anything at all (or maybe a random char, depending on what the contents of your memory are for the moment). Fourth, even if all the prevoius errors are corrected, the string generated in your function contains a lot of words that you might not want to use every time you display the station of choice. Example: if choice1 = "You have selected Canonbury Cross ", your first output (choice1 + " as your arriving station.") would generate the output: You have selected Canonbury Cross as your arriving station. .
Later, when you want do display them both, and you generate the text ("You have selected " + choice1 +" and "+ choice2 + " as your stations") The following output would be generated: You have selected You have selected Canonbury Cross and You have selected Edbury as your stations Think about that for a moment. Last, and fifth (I think), you would need to print a string of chars (%s) and not one char (%c) in order to see all letters in your variables. Pheew.

A couple things,
1) Get rid of the system("CLS") . It's in your way, you can't see what's happening. If you really need them for your instructor, add them later, otherwise don't use them at all.

2) Look carefully at what your stationsswitch() function is returning.

Thanks very much everyone. Lots to look at there. I'll go away now i've finished my night shifts at work and try to work out some solutions and get a bit further with it. Very good info from you which is very appreciated. I think i have a better idea of what i need to do now. If i get stuck i'll be sure to come back :) Thanks again.

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.