How would I write this in C?

May 28, 2017 at 5:57pm
I'm currently in intro to programming and I am very confused as how to write this in C, I would appreciate an explanation too if possible:

1. Ask the user for an integer. Make sure all variables are appropriately initialized to zeros, i.e., integer variables should be equal to 0 and floating-point variables should be equal to 0.0.
2. Get the integer and store it into a variable.
3. Ask the user for a floating-point number.
4. Get the number and store it into a variable.
5. Display a blank line, i.e., separate the output from the input.
6. On its own line, display a message along with the integer on the screen.
7. On its own line, display a message along with the number to three decimal places on the screen.
8. Display a blank line.
9. On its own line, display a message with your name in it.

This is what I have so far, but I don't know what to do from here:

#include<stdio.h>
#include<stdlib.h>
int main()
{
printf("Enter an integer: \n");
system("pause");
}
Last edited on May 28, 2017 at 6:08pm
May 28, 2017 at 6:04pm
you are writing C. C++ uses cout instead of printf and those are C includes.

I will get you started...


int I = 0;
cout << "enter integer" << endl; //appends end of line #5
cin >> I;
cout << "int value is " << I << endl;

float f;
... similar code... you need to figure out floating point cout formatting, use google for setw and float formatting or check your book

I think the above will get you all 9 items, with a little digging. Code something, try it, play with it, google at it, see what you can come up with ... that is how you learn it.



May 28, 2017 at 6:08pm
Thank you for the fast response, but I have made a mistake. It is supposed to be written in C not C++
May 29, 2017 at 3:33am
http://en.cppreference.com/w/c/io/fscanf
http://en.cppreference.com/w/c/io/fprintf

Have a look at these links.

Note that when the assignment specifies "floating point", this doesn't mean you have to use float. float, double and long double are all floating point types; prefer double because the precision of float is easily exceeded.

Make sure you get the format specifiers for scanf and printf correct. Also make sure you check the value returned by scanf , just to see that it worked. Also make sure you initialise your variables to something when you declare them.

Good Luck !!
Topic archived. No new replies allowed.