Not sure what the Problem is here

This is a discussion on Not sure what the Problem is here within the C++ Programming forums, part of the General Programming Boards category; Hello, I'm having an issure with this section of code. It keeps crashing after I enter the employee number. I ...

  1. #1
    Registered User Vireyda's Avatar
    Join Date
    Mar 2004
    Posts
    26

    Question 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

  2. #2
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    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?

  3. #3
    Registered User Vireyda's Avatar
    Join Date
    Mar 2004
    Posts
    26
    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

  4. #4
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    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.

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,418
    > 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.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 04:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 10:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21