A very basic problem about input output

  • Thread starter Thread starter Shing
  • Start date Start date
  • Tags Tags
    Input Output
Click For Summary
SUMMARY

The discussion centers on a C programming assignment that requires the implementation of multiple functions for input and output of different data types: character, integer, float, and double. The provided code attempts to define these functions but contains several syntax errors and logical issues, such as incorrect variable names and improper use of the scanf function. The main function is designed to prompt the user for input and call the appropriate functions based on the user's choice, but it fails to execute correctly due to these errors.

PREREQUISITES
  • Understanding of C programming syntax and structure
  • Familiarity with data types in C: char, int, float, double
  • Knowledge of input/output functions in C, specifically printf and scanf
  • Basic debugging skills to identify and fix syntax errors
NEXT STEPS
  • Review C programming function definitions and scope
  • Learn about error handling in C, particularly with user input
  • Explore best practices for using fflush(stdin) and its alternatives
  • Study the correct usage of scanf for different data types in C
USEFUL FOR

Students learning C programming, educators teaching programming fundamentals, and developers looking to refine their input/output handling skills in C.

Shing
Messages
141
Reaction score
1

Homework Statement





Write a code with the following functions:



Function 1: print a list of 4 different type variables input and output:

character, integer, float, and double.

Function 2: prompt and return an input character.

Function 3: print out a given character.

Function 4: prompt and return an input integer number.

Function 5: print out a given integer number.

Function 6: prompt and return an input float number.

Function 7: print out a given float number.

Function 8: prompt and return an input double number.

Function 9: print out a given double number.


The Attempt at a Solution




# include <stdio.h>

void do_list(void)
int main ()

{ printf(" Choose to input a number among the following types:\n");
printf(" 1. a character\n");
printf(" 2. an integer\n");
printf(" 3. a float\n");
printf(" 4. a double\n");
return;
}
//====diagram ========

{ char achar;
int aint;
float afloat;
double adouble;
do_list();
printf("Input your chioce");
scanf("%d", &choice);

if(choice == 1) {
achar = input_char();
print_char(achar);

}

else if(choice == 2) {
ainteger = input_integer();
print_integer(ainteger);

}

else if(choice == 3) {
afloat = input_float();
print_float(afloat);
}

else(choice == 4) {
adouble = input_double();
print_double(adouble);
}

}
return 0;


//====main function I========

char input_char(void)

{
char achar;
printf("Type your character:");
fflush(stdin); //clean the input uffer area.
scanf("%c",&a);
return achar;
}

void print_char(char a)

{
printf(Your input is "%c\n",a);

return;
}

//====main function II========


int input_int(void)

{
int aint;
printf("Type your character:");
fflush(stdin); //clean the input uffer area.
scanf("%c",&b);
return aint;
}

void print_int(int b)

{
printf(Your input is "%c\n",b);
return;
}

//====main function II===========

float input_float(void)

{
float afloat;
printf("Type your character:");
fflush(stdin); //clean the input uffer area.
scanf("%f"",&c);
return afloat;
}

void print_float(float c)

{
printf(Your input is "%c\n",c);
return;
}

//=====main function III==========

double input_float(void)

{
double adouble;
printf("Type your character:");
fflush(stdin);
scanf("%c",&d);
return adouble;
}

void print_double(double d)

{
printf(Your input is "%c\n",d);
return;
}
 
Physics news on Phys.org
It seems to me that the program will only print the list and then return.

But what exactly was your question?
 

Similar threads

  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 19 ·
Replies
19
Views
3K
Replies
9
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 9 ·
Replies
9
Views
4K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 12 ·
Replies
12
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K