Sinisterly
1st C Program - Printable Version

+- Sinisterly (https://sinister.ly)
+-- Forum: Coding (https://sinister.ly/Forum-Coding)
+--- Forum: C, C++, & Obj-C (https://sinister.ly/Forum-C-C-Obj-C)
+--- Thread: 1st C Program (/Thread-1st-C-Program)



1st C Program - zipzap171 - 08-07-2011

Was kinda proud of myself when I did this, of course it's nothing special. I like to think of it as a good example of using If Else loops to have a recurring statement until the right user input is given.(see code 1) On a side note, I'm trying to learn more of C and I'm looking for an easier way to loop a code, say a multiplication code. Right now I'm setting impossible to meet conditions on a while loop but am wondering if theirs something more efficient or useable. (see code 2)

Code:
#include <stdio.h>

int main()
{
    int a;
    printf ("Insert the right number: \n");
    scanf ("%d", &a);
    if (a==10){
    printf ("Correct!\n");
    getchar();
}
    else{
    int a;
    do{
        printf ("Try again\n");
    scanf("%d", &a);
} while (a!=10);
    if (a==10){
               printf ("Correct!\n");
    getchar();
}
}
getchar();
}

Code:
#include <stdio.h>

int mult (int a, int b);

int main()
{
    int a;
    int b;
    
    printf("Insert two numbers to multiply\n");
    while (a>=0 || a<=0) {
    scanf("%d", &a);
    scanf("%d", &b);
    printf("This quals: %d\n", mult (a, b));
}
getchar();
}                          
int mult (int a, int b)                  
{
    getchar();
    return a*b;
}



RE: 1st C Program - sec0verun - 08-07-2011

not bad bro try for loops


RE: 1st C Program - Frooxius - 08-07-2011

Not bad for starters, though if I might recommend something, it's better formating the code - proper indentation of code blocks helps to improve to readability of the code (good code should be easy to read), for example I thought for a second that the "else" statement was assigned with the int main() block because of improper formatting, which is of course nonsense.

The code should always be indented when you open a new code block (between curly brackets { } ) and then return to the previous indentation after the block.

Here, I reformatted the code, I didn't change the code itself anyhow, though that can be improved too, but I really need to go to sleep now :lol:

Code:
#include <stdio.h>

int main()
{
    int a;
    printf ("Insert the right number: \n");
    scanf ("%d", &a);
    if (a==10)
    {
        printf ("Correct!\n");
        getchar();
    }
    else
    {
        // int a; read note below, why is this not needed
        do
        {
            printf ("Try again\n");
            scanf("%d", &a);
        } while (a!=10);
        if (a==10)
        {
            printf ("Correct!\n");
            getchar();
        }
    }
    getchar();
}

Why is the second int a not needed: it is not required, you already declared variable a before, this only creates another one with the same name, valid only for this block, but the old one is still valid too, only gets hidden.

Code:
#include <stdio.h>

int mult (int a, int b);

int main()
{
    int a;
    int b;
    
    printf("Insert two numbers to multiply\n");
    while (a>=0 || a<=0)
    {
        scanf("%d", &a);
        scanf("%d", &b);
        printf("This quals: %d\n", mult (a, b));
    }
    getchar();
}
                          
int mult (int a, int b)                  
{
    getchar();
    return a*b;
}




RE: 1st C Program - zipzap171 - 08-07-2011

Thanks for that, makes sense though, neater makes it easier to read. The int a thing was a forgetful thing on my part, I remember learning I didn't have to declare it again I just kinda forgot XD.


RE: 1st C Program - Frooxius - 08-07-2011

Yeah, the second int a was technically valid (but not needed) according to newest standard (older requires all declarations to be on the beginning and doesn't permit declarations inside blocks), because it was in a nested code block, meaning it would be valid only for that block and it would hide the original variable a (though you can access it by typing ::a) so you basically had two a's at the same time (though the newer one would cease to exist as soon as the current block would be exited).

However if you declared it on the same level as the original int a (outside the else block) you would certainly get a compile time error.

It's one of the tricky things about C/C++, that might confuse inexperienced programmers, I heard a lot that they're considered to be very difficult languages to learn, though I don't know that much about that, they didn't seem difficult to me.


RE: 1st C Program - chipp - 08-07-2011

@frooxius:
Code:
do
        {
            printf ("Try again\n");
            scanf("%d", &a);
        } while (a!=10);
        if (a==10) //why would you do this as it has been checked in do while loops?

@zipzap: btw, if you want to learn c++, you can go here:

cyberianzone.blogspot.com

it's in indonesian though, but you can download the book...


RE: 1st C Program - Frooxius - 08-07-2011

chipp: That's not my code, that's what zipzap171 originally wrote, I only reformatted the same code. I didn't want to modify the functionality (though I mentioned in my post that can be improved too) so he can see the exactly same code formatted more properly. If I started removing/changing things, it might make it more confusing.

I didn't have time to edit the code itself, because it was about 6 am to me and I needed to go to sleep :lol:

Though I might do it right now:

Code1, same functionality, but it's much simpler and the condition is always tested only once. The for loop firsts read the character, then tests the condition. If it's not true, it executed the code below it (prints Try again) and then executes the third parameter, which reads another character and then tests the condition again. If the condition is true, the associated code won't run and execution flow will skip to printf("Correct!\n");

Code:
#include <stdio.h>

int main()
{
    int a;
    printf ("Insert the right number: \n");
    for(scanf("%d", &a); a != 10; scanf("%d", &a))
        printf("Try again!\n");
    printf("Correct!\n");

    getchar(); getchar(); // needs to be twice here, second one captures the '\n' in the stdin stream
}

Doesn't need much changing (though you can do the same without need for a function), however, NEVER put any code to a function, that's unrelated to what the function does. The getchar() shouldn't be there. Functions should never be tied to a specific context in which they're executed: they should do what their name says, nothing more.
Code:
#include <stdio.h>

int mult (int a, int b);

int main()
{
    int a, b; // you can declare two or more variables of the same type like this
    
    printf("Insert two numbers to multiply\n");
    while (a != 0)    // basically the same thing, though I guess you wanted to try the || operator
    {
        scanf("%d", &a);
        scanf("%d", &b);
        printf("This quals: %d\n", mult (a, b));
        // printf("This quals: %d\n", a*b);  // no function needed
    }
    
    getchar(); getchar(); // again, it's needed twice, because there's still '\n' - Enter in the input stream, so it gets captured by first and the second one can then wait for another character
}
                          
int mult (int a, int b)                  
{
    /* getchar(); this shouldn't be here, function should do only what its name says, independently on the rest of the code
       if function multiplies two numbers, I would expect it to do just that, nothing more.    */
    return a*b;
}

EDIT Also if I might recommend some book, it's certainly C++ The Complete Reference, by Herbert Schildt, it provides great explanation of the whole C/C++ standard and some of the standard libraries and contains lots of examples. It also works as great reference when programming.


RE: 1st C Program - zipzap171 - 08-07-2011

Thanks for the edits and tips, I'll take a look at the book you mentioned. Right now C doesn't really seam that hard to understand but I'm sure it gets more difficult later on. It's much better to have feed back from people who understand it then just trying to figure something out when playing around with what little knowledge an online guide has given.


RE: 1st C Program - Frooxius - 08-07-2011

Well there's a lot going on in the C/C++ language, that might not be clear at the first sight, but might cause some problems in certain situations. Actually there are even situations where you need to know some assembly to really understand exactly what's going on: of course, you don't need to understand it to avoid these problems, you simply learn how to avoid them, but because C/C++ is quite close to the actual machine language, compared to many other high level languages, it gives a lot of power if you realize how to use it, but also a lots of problems/potential bugs.