Newbie here


Hey guys, I'm new here. Do you mind telling me if this code below me a executable program. Since I don't have any software yet that can execute a C++ codes, I just want to post this here. Leave me hints for me to figure out the flaw of the code below me. Thanks

#include<stdio.h>
int main ( )
{
int numyrs;
float intrst;
printf("Input variable");
scanf("%f", &numyrs);

if (numyrs>=5)
printf (".045");

else if ((numyrs<5) && (numyrs>=4))
printf("0.4");

else if ((numyrs<4) && (numyrs>=3))
printf("0.35");

else if ((numyrs<3) && (numyrs>=2))
printf("0.3");

else if ((numyrs<2) && (numyrs>=1))
printf(".025");

else (numyrs<1)
printf(".02");

return 0;
}
It is...but there is a problem in it.
can you tell me?
think about your scanf. . . then think about the variable that you scanned.
Also, Why do you have two variables?
Last edited on
instead of

scanf("%f", &numyrs);


try the
scanf("%d", &numyrs);


Heya!

I'm a beginner myself so maybe i don't see it but why would you write for example:
else if ((numyrs<5) && (numyrs>=4))
when it's exactly the same thing as:
else if (numyrs == 4)
Yes but Nsanden, that number is a floating point number meaning it is allowed a decimal point. So it could be 4.0 - 4.9, which would still be more than or equal to four, but less than five.

My mistake, numyrs is an integer. I think the error is not with the scanf but with the declaration of numyrs.

gcc numyrs.cpp -lstdc++ -o numyrs g++ numyrs.cpp -o numyrs
This compiles ok:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <cstdio> // Files in the stl do not have .h extensions, e.g. iostream or fstream. stdio.h is a C header, so it is prepended with a c, e.g. cstdlib or cmath.

int main () {
    float numyrs;
    // float intrst; this is never used
    printf("Input variable\n");
    scanf("%f", &numyrs);

    if (numyrs >= 5)
        printf (".045");

    else if (numyrs < 5 && numyrs >= 4) // numyrs between 4.0 and 4.9 (inclusive)
        printf("0.4");

    else if (numyrs < 4 && numyrs >= 3)
        printf("0.35");

    else if (numyrs <3 && numyrs >= 2)
        printf("0.3");

    else if (numyrs < 2 && numyrs >= 1)
        printf(".025");

    else // (numyrs<1) <-- this isn't an else if statement, with it you get compile errors.
        printf(".02");
    printf("\n"); // Print a newline

return 0;
}


Also, I tested it and does work.
Last edited on
Guess what, you can get yourself a copy of Microsoft VC++ Express Edition for free. Here's a link http://www.microsoft.com/express/vc/ This way you will be able to test code without posting it here and ask some other guys to test it for you.
Good idea, install some proprietary software that requires you to sign up to a mailing list to use for more than a month, isn't standards-compliant, is severely bloated and takes ages to install and configure.

Or you can download gcc or MinGW and a text editor, or just download an IDE...

Codeblocks is good imo.
Uhhh, VC++ is fine. I did not have to sign up for a mailing list, it wasn't hard to configure at all. In fact, I didn't really have to change anything.

Plus you get a great debugger with it. And if you want to go into Win32 development, it's quite handy.

Also, what about it exactly isn't standards complaint? I haven't found anything abnormal about it...
It conforms to Microsoft standards. But thinking about it any compiler will if you make a win32 GUI.

It's not difficult to configure, but when you first run it, it spends ages configuring itself.

The GUI, while very nice, is just that - eye candy. And its cluttered.

But my issue with it is mainly how resource hungry and bloated it is.

I'll give, the debugger is good, but I still don't see the point in saying 'Get this.' Rather than post a few options with a footnote saying that you think x is the best. But to just roll out the MS IDE, when you don't even know what platform the other person is running.
Anyway, never mind.
Thanks a lot guys. Much appreciated. Really
Topic archived. No new replies allowed.