Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > help on basic c program

help on basic c program
Thread Tools
Fresh-Faced Recruit
Join Date: Feb 2001
Status: Offline
Reply With Quote
Sep 17, 2001, 02:26 AM
 
Could you guys take a look at the following and tell me what's wrong with it. I can get it to compile using cc in Terminal, but the second scanf line does not store values correctly when i execute the program. Well, at least not on my machine. When I telnet into a school unix machine, the code compiles and performs exactly how it should. Any ideas?

#include <stdio.h>

int main()
{
char cmpnd_code, first_name, last_name;

int num_months, input_status, act_num;

float invest_amt;

/* Prompt for personal and account information */

printf( "Entert1st lettter of first name (char) and last name (char)\n" );
printf( "\taccount number (int), investment amount (float) n");
fflush(stdout);

input_status = scanf( "%c %c %d %f", &first_name, &last_name, &act_num, &invest_amt );
fflush(stdin);

/* verify initials, account number, investment amount */

printf( "\nNumber of values scanned and stored = %d\n", input_status );

printf( "\nVerifying Inputn first initialt\t %c\n last initialt\t %c\n",
first_name, last_name );
printf( " account numbert %d\n investment amountt %f\n\n",
act_num, invest_amt);

/* Prompt for compound interest character code and number of months */

printf( "Enter: compound interest code (char) and number of months (int)n" );
fflush(stdout);

input_status = scanf( "%c %d", &cmpnd_code, &num_months );
fflush(stdin);

/* verify compound interest code and number of months */

printf( "\nNumber of values scanned and stored: %d\n", input_status );

printf( "\nVerifying Inputn compound interest code: %c\n number of monthst %d\n\n", cmpnd_code, num_months );

return 0;
}
     
Junior Member
Join Date: Mar 2001
Status: Offline
Reply With Quote
Sep 17, 2001, 07:58 PM
 
First of all, never call fflush on an input stream, as this produces undefined behavior. Get rid of fflush(stdin) everywhere it appears. And while you're at it, get rid of fflush(stdout) too, because you are careful to always print a newline at the end of your lines, which automatically flushes the stream.

Now, the problem with your code is as follows. When I enter the data in the first scanf, I hit return afterwards, appending a '\n' to stdin, and the second scanf is picking up the '\n'. It sees that '\n' and scans that into the char, so cmpnd_code is always set to '\n'. Then it tries to scan an int, but instead it typically hits a char such as x, and it fails.

The solution is real easy. Add an extra space after the first quote in the second scanf string. That is, change

input_status = scanf( "%c %d", &cmpnd_code, &num_months );

to

input_status = scanf( " %c %d", &cmpnd_code, &num_months );

Here's why this works. When scanf encounters any whitespace character, it discards ALL whitespace characters in stdin until it encounters a non-whitespace character. So that extra space gets rid of the '\n' character in stdin.

Hope this helps.

-Peter
     
jgm50d  (op)
Fresh-Faced Recruit
Join Date: Feb 2001
Status: Offline
Reply With Quote
Sep 18, 2001, 12:31 AM
 
Thank you so much...you have made my life a lot easier. I'm a beginning comp sci student and you just saved my grade. Thanks again.
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 12:16 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2