First Exercise
started by: L GeeL Gee
on: 1211093410|%e %b %Y, %H:%M %Z|agohover
number of posts: 3
rss icon RSS: new posts
First Exercise
L GeeL Gee 1211093410|%e %b %Y, %H:%M %Z|agohover
#include <cstdio>

int main()
{
    int number = 0;
    int number2 = 0;
    int sumofnumber = 0;
    int xofnumber = 0;
    float divideofnumber = 0;
    printf("Put a number in here!");
    scanf("%d", &number);
    fflush(stdin);
    printf("Put another number in here!");
    scanf("%d", &number2);
    fflush(stdin);
    printf("I got the numbers %d and %d!\n", number, number2);
    sumofnumber = number+number2;
    printf("The sum of your two numbers is %d!\n", sumofnumber);
    xofnumber = number * number2;
    printf("The product of your two numbers is %d!\n", xofnumber);
    divideofnumber = ((float)number / (float)number2);
    printf("The division of your two numbers is %10.10f!\n", divideofnumber);
    scanf("*");
    return 0;

}

This is the first exercise which Jackson asked us to do with a few added bits. How do i clear the screen after the user has input the two numbers.

last edited on 1211093464|%e %b %Y, %H:%M %Z|agohover by L Gee + show more
unfold First Exercise by L GeeL Gee, 1211093410|%e %b %Y, %H:%M %Z|agohover
Re: First Exercise
JGatJGat 1211094279|%e %b %Y, %H:%M %Z|agohover

Clearing the screen… I'm not sure that can be done too easily. The only thing I can think of is printing a lot of \n until it all disappears. Tom might know a better way.

Good work on the code. A couple of unnecessary bits though:

#include <cstdio>
 
int main()
{
    int number = 0;
    int number2 = 0;
    int sumofnumber = 0;
    int xofnumber = 0;
    float divideofnumber = 0;
    printf("Put a number in here!");
    scanf("%d", &number);
    fflush(stdin);  // Only need this before a scanf("*"); (i.e. a scanf that is interested in a new-line character). It can stay there, but it won't change anything not being there.
    printf("Put another number in here!");
    scanf("%d", &number2);
    fflush(stdin);  // This can go anywhere between the scanf on the line above and the scanf("*"); - it's personal preference where you choose
    printf("I got the numbers %d and %d!\n", number, number2);
    sumofnumber = number+number2;
    printf("The sum of your two numbers is %d!\n", sumofnumber);
    xofnumber = number * number2;
    printf("The product of your two numbers is %d!\n", xofnumber);
    divideofnumber = ((float)number / (float)number2);
    printf("The division of your two numbers is %10.10f!\n", divideofnumber);
    scanf("*");
    return 0;
 
}
 
/* Another little random fact:
   you can do operations within a printf command i.e.:
   printf("%d", number + number2);
   I just didn't want to tell you that so you could have a go at assigning (i.e. sum = x + y)
   Also, these little notes are comments. We'll get into them in more detail next week.
*/
unfold Re: First Exercise by JGatJGat, 1211094279|%e %b %Y, %H:%M %Z|agohover
Re: First Exercise
Michael Bauer 24Michael Bauer 24 1211100267|%e %b %Y, %H:%M %Z|agohover

Yes its quicker to do your math in the input and output statements however it is generally good practice to assign it a variable. I would put the code in a loop but I guess Jackson hasn't taught you that yet.

unfold Re: First Exercise by Michael Bauer 24Michael Bauer 24, 1211100267|%e %b %Y, %H:%M %Z|agohover
new post
Unless stated otherwise Content of this page is licensed under Creative Commons Attribution-NonCommercial 3.0 License