Question #48479

Write a program that asks the user to enter a character, a string, an integer, and a float value using function overloading. The user should be prompted for each data item in a separate function with the name getData and a reference parameter of the type item being read. Display the data entered twice, using a different function but each time named displayData, that have different parameter lists.
The following is a sample run of the program.

Enter a character: A
Enter a word: sunny
Enter a whole number: 125
Enter a number with decimal point: 50.25

You entered the following data:
Letter: A
String: sunny
Integer: 125
Float: 50.25

Your data displayed again:
Integer: 125
Float: 50.25
Letter: A
String: sunny

Expert's answer

Answer on Question#48479 - Programming - C


#include <stdio.h>
int getData(char &);
int getData(char *);
int getData(int *);
int getData(float *);
void displayData(char);
void displayData(char*);
void displayData(int);
void displayData(float);
int main(){
char c;
char string[200];
int i;
float f;
getData(c);
getData(string);
getData(&i);
getData(&f);
printf("\nYou entered the following data:\n");
displayData(c);
displayData(string);
displayData(i);
displayData(f);
printf("\nYour data displayed again:\n");
displayData(i);
displayData(f);
displayData(c);
displayData(string);
getchar(); getchar();
return 0;
}
int getData(char &c){
printf("Enter a character: ");
return scanf("%c", &c);
}
int getData(char *str){
printf("Enter a word: ");
return scanf("%s", str);
}
int getData(int * i){
printf("Enter a whole number: ");
return scanf("%i", i);
}
int getData(float * f){
printf("Enter a number with decimal point: ");
return scanf("%f", f);
}
void displayData(char c){
printf("Letter: %c\n", c);
}
void displayData(char *str){
printf("String: %s\n", str);
}
void displayData(int i){
printf("Integer: %i\n", i);
}
void displayData(float f){
printf("Float: %f\n", f);
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

LATEST TUTORIALS
APPROVED BY CLIENTS