Reference no: EM132225804
Programming Assignment -
Where c is a character variable, the scanf( ) function scanf("%c",&c1) will read in one character from the keyboard. If the user hits the <Q> key (for lowercase 'q') followed by the <Enter> key, that action constitutes two characters from the keyboard: 'q' and '\n'. The <Enter> key generates the '\n' character.
Another important fact, is that characters entered from the keyboard that are not yet read by a scanf( ) will sit in a keyboard queue waiting to be read by the next scanf( ) in your program.
For example:
If you have a program that contains:
printf("Please enter a single character from the keyboard\n");
scanf("%c",&c1);
scanf("%c",&c2);
then c1 will contain the character from the keyboard, and c2 will contain '\n' generated by the <Enter> key.
If you have a program that contains:
printf("Please enter two characters from the keyboard\n");
scanf("%c",&c1);
scanf("%c",&c2);
then c1 will contain the character from the keyboard, and c2 will contain the 2nd character. The '\n' generated by the <Enter> key will be waiting in a queue to be read by the next scanf("%c", ) in your program.
If you have a program that contains:
printf("Please enter two characters from the keyboard.\n");
scanf("%c",&c1);
scanf("%c",&c2);
printf("Please enter two more characters from the keyboard\n");
scanf("%c",&c3);
scanf("%c",&c4);
then c3 will contain the '\n' from the first <Enter> key.
The same holds when reading a number from the keyboard. Hitting the <Enter> key to enter the number generates a '\n' character that is not part of the number that is read from the keyboard.
If you have a program that contains:
printf("Please enter an integer.\n");
scanf("%d",&i1);
printf("Please enter a character.\n");
scanf("%c",&c1);
Variable i1 will contain the integer, but character variable c1 will always contain the '\n' from the <Enter> key following the integer.
So, if you have a program that contains:
printf("Please enter an integer.\n");
scanf("%d",&i1);
scanf("%c",&c1);
printf("Please enter a character.\n");
scanf("%c",&c2);
Variable i1 will contain the integer, character variable c1 will contain the '\n' from the <Enter> key, character variable c2 will contain the character entered by the user, and the 2nd '\n' from the 2nd <Enteer> key will be waiting in the keyboard queue to be read by the next scanf( ) in your program.
Using what you have learned above, write a program to input a list of names and account balances from the keyboard and write them into a file called "out.txt".
(1) Open an output file called "out.txt" for writing. The file "out.txt" will be located in the same directory from which the executable from your program will be run.
(2) Ask the user if they want to enter another name and account balance. Have the user respond with either 'y' or 'n'.
(3) If the user responds with 'n', close the file and end the program.
(4) If the user responds with 'y', instruct the user to enter the last name and hit the <Enter> key. Read this last name and write it to the output file followed by a comma and a blank space.
Then instruct the user to enter the first name followed by the <Enter> key. Read this first name and write it to the output file on the same line followed by a comma and a blank space.
Then instruct the user to enter the account balance as a floating point number followed by the <Enter> key.
Read this value and write it to the same line, but preceded by the dollar sign '$' and printed with exactly 2 digits past the decimal point.
(5) Go to (2) and repeat till the user is done entering the list of names and account balances.
For example, your file out.txt will look like
Doe, Jane, $211.33
Smith, John, $17.44
de Mills, Cecil, $557.44