# Not sure what the Problem is here

• 04-12-2004
Vireyda
Not sure what the Problem is here
Hello,

I'm having an issure with this section of code. It keeps crashing after I enter the employee number. I have the sense that the value isn't being assigned to the variable FindNumber, but I'm unsure. If anyone can offer any help on this matter, it would be greatly appreciated.

Thanks,
Vireyda

Code:

```void ChangeCosts(struct TravelCosts expenses[], int num)         {         int i, FindNumber;         char update;         printf("Enter Employee number to find record: ");         fflush(stdin);         scanf("%d", FindNumber);                 for(i=0; i<num; ++i)                 {                 if(FindNumber==expenses[i].emp_num)                         {                         printf("What would you like to update?");                         printf("Distance Traveled\t- D or d\nMileage Rate\t- R or r\nMeal Costs\t- M or m\nQuit to Main Menu\t- Q or q");                         fflush(stdin);                         scanf("%c", update);                         switch(update)                         {case 'D': case 'd':                                 do                                 {                                 printf("\nPlease enter new distance traveled(400km max): ");                                 scanf("%f", expenses[i].distance_traveled);                                 if(expenses[i].distance_traveled>400)                                 printf("\nThat distance exceeds the 400km maximum.  Please try again.\n");                                 }while(expenses[i].distance_traveled>400);                                 break;                         case 'R': case 'r':                                 do                                 {                                 printf("\nPlease enter new Mileage Rate(From \$0.30 to \$0.40): \$");                                 scanf("%f", expenses[i].mileage_rate);                                 if(expenses[i].mileage_rate<.30&&expenses[i].mileage_rate>.40)                                 printf("\nThat mileage rate does not fall between \$0.30 and \$0.40.  Please try again.\n");                                 }while(expenses[i].mileage_rate<.30&&expenses[i].mileage_rate>.40);                                 break;                         case 'M': case 'm':                                 do                                 {                                 printf("\nPlease enter new Meal Costs(\$25.00 max):  \$");                                 scanf("%f", &expenses[i].meals);                                 if(expenses[i].meals>25)                                         printf("That amount exceed the \$25.00 maximum.  Please try again.\n");                                 }while(expenses[i].meals>25);                                 break;                         case 'Q': case 'q':                                 break;                         default:                                 printf("\nThat selection is invaild.  Please try again.\n");                         }//end switch                         }//end if                 else                         printf("\nThere is no record of that employee number in the database.\n");                 }//end for         }//end ChangeCosts```
• 04-12-2004
jlou
More problems with pointers. :)

> scanf("%d", FindNumber);

should be

scanf("%d", &FindNumber);

Remember, FindNumber is an int, and scanf wants a pointer, so you must use the address of operator (the "&") to get a pointer to the int. Go through every place you call scanf, and if you have an integer variable, add the "&" to it. Your last thread was a special case in which your variable was already a pointer, which is why you didn't need the "&" there.

By the way, since this is C code, shouldn't you be posting these questions on the C forum instead of the C++ forum?
• 04-12-2004
Vireyda
Thanks for the help, but I was under the impression that I was using C++. The course I'm enrolled in is called intro to C++ I believe.

Vireyda
• 04-12-2004
jlou
Well, since nearly all of C is also valid in C++, technically your code is also C++. However, they are really two different languages in terms of how they are used. While it is common to teach C as an introduction to C++, many believe that is a bad idea (including the creator of C++, Bjarne Stoustrup [pdf]). It sounds like your class does teach C code as an introduction to C++.

That kind of sucks for you, if you really want to learn C++, because unless you are careful, it is easy to use C techniques in a C++ program when there are better options available. An example is scanf and printf. The rough equivalent to those in C++ is cin and cout, which I'm sure you will learn about if this is really a C++ class. Of course, since C++ includes nearly all of C, scanf and printf work in a C++ program. That doesn't make them better. In most cases cin and cout would be preferred because they are somewhat easier to use and harder to mess up.
• 04-13-2004
Salem
> The course I'm enrolled in is called intro to C++
If that tutor told you fflush(stdin) was a good idea, I'd consider leaving the course and getting my money back. How many other bits of mis-information are they going to "teach".
What course book does the tutor follow?

As jlou has said, this is C code, the only thing making it C++ is the // comments.