I\'m just starting out in C, and have only learned if statements, while loops, a
ID: 3626838 • Letter: I
Question
I'm just starting out in C, and have only learned if statements, while loops, and how to use scanf, printf, int, char, float, and few others.the assignment i am tasked to write, for example, asks to take a few inputs from the user and do some calculatoin then outputs something else. Take for example a program that looks like this when it is run (say from visual studio during testing):
What is your teachers name?
<user input>
How many classes is your teacher conducting this semester?
<user input>
How many years has your teacher been teaching?
<user input>
Your teacher is named <whatever user inputted for name>, has been teaching for <number of years>, and taught approximately <number of classes teaching times years teaching> based on the information you submitted.
This is not the program i need to write, but only an example of what i need. My main questions are, how do i ask for a name that is not numbers (like only characters), and have the system read it, then output it later when instructed to?
Also, how do i instruct the computer to ONLY output up to TWO decimal places. for example what if the user inputted "3.5678" for years taught. Should I use a loop that would ask the user to reinput using only up to two decimal places. less than two decimal places is fine (like 3.51, or 3.4, or 3123).
thanks!
Explanation / Answer
for getting names u have to use character arrays as follows
char name[50];
above name is a variable that can hold up to 50 characters.
ask user name using printf
printf(" Enter your name ");
scanf("%s",name); now u will save name in name variable
infact while scanning u can scan upto only two decimal places.
float a;
scanf("%.2f",&a);
now a will read only upto 2 decimals points.
if u want any more help .i will.
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.