Scheduled Maintenance: We are aware of an issue with Google, AOL, and Yahoo services as email providers which are blocking new registrations. We are trying to fix the issue and we have several internal and external support tickets in process to resolve the issue. Please see: viewtopic.php?t=158230

 

 

 

Interesting tutorial

Programming languages, Coding, Executables, Package Creation, and Scripting.
Message
Author
cuckooflew
Posts: 677
Joined: 2018-05-10 19:34
Location: Some where out west
Been thanked: 1 time

Re: Interesting tutorial

#21 Post by cuckooflew »

So anyway, back to C , here is something else I found, I did not write it, but don't really know who did, any way, :

Code: Select all

   #include <stdio.h>
   #include <stdlib.h>
   int main (void)
   {
    char name[20];
    printf("Hello. What's your name?\n");
    fgets(name,20,stdin);
    printf("Hello, %s", name);
    int i;
    int response;
    int correctAnswers = 0;
    int incorrectAnswers = 0;

    printf("\nMath Quiz\n");
    printf("Please enter # of problems you would wish to try:");
    scanf("%d", &response);

    if(response == 0)
    {
        printf("\nThanks for playing!\n");
        return 0;
    }

    for(i=0; i<response; i++)
    {
        int answer = 0;
        srand(time(0));
        int a = rand() % 12;
        int b = rand() % 12;
        printf("\n%d * %d = ",a ,b);
        scanf("%d", &answer);
        if((a * b) == answer){
            printf("\nCongratulations You are correct!\n");
            correctAnswers++;
        }
        else{
            printf("Sorry you were incorrect!\n");
            incorrectAnswers++;
        }

    }
    printf("\n\nYour Results:\n\n\n");
    printf("Number Incorrect: %d\n", incorrectAnswers);
    printf("Number Correct: %d\n", correctAnswers);
    if(correctAnswers > incorrectAnswers){
        printf("You Passed!  \nGood work!\n\n", name);
    }
    else{
        printf("You did not pass!\nYou need more work!\n\n");
    }

    return 0;
} 
There is a problem with it though, it does not remember the "name",

Code: Select all

printf("You Passed!  \nGood work!\n\n", name); 
On this line, ....
Also a question about when we compile,... for example on this, to compile use:

Code: Select all

math2 compile instruction
gcc math2.c -lpthread 
to run it: ./a.out
 

On the earlier example, we used :

Code: Select all

gcc -o hello hello.c 
use ./hello to run
The question is, what determines the compile method, ? How do we not what method to use ? I suppose this might be answered in the gcc manual, and the options we use.

Code: Select all

  -o file
           Place output in file file.  This applies regardless to whatever
           sort of output is being produced, whether it be an executable file,
           an object file, an assembler file or preprocessed C code.

           If -o is not specified, the default is to put an executable file in
           a.out, the object file for source.suffix in source.o, its assembler
           file in source.s, a precompiled header file in source.suffix.gch,
           and all preprocessed C source on standard output.
 
Ahh ok, well that makes sense, and also explains why using

Code: Select all

gcc -o math math.c  
worked the same, except for the out put,...hmm.
Now, I know this would seem extremely boring to more advanced "programmers", and even to a beginner, but for the beginner, these are simple examples, and you should be able to follow them , and "build" your first working programs, if you can not, then of course you need to show what you did, and ask. To me , it was exciting, when my first programs actually worked, and I found that I stayed more interested, and was eager to continue when I had code that actually worked, where at first, before I knew better, I tried jumping into advanced code, and not anything I wrote, just copy/paste,...so when it did not work, I had no clue as to why, and it was just plain frustrating, I quickly gave up, saying "this is too complicated for me". Hope fully this makes sense, and it is why we say "Learn to crawl before you try to walk", When a baby is forced to try walking, to soon, they fall down, alot, and get discouraged, and maybe even scared to even try to stand, and walk, when they should be able to. .....guess for now that is about it.
Please Read What we expect you have already Done
Search Engines know a lot, and
"If God had wanted computers to work all the time, He wouldn't have invented RESET buttons"
and
Just say NO to help vampires!

LE_746F6D617A7A69
Posts: 932
Joined: 2020-05-03 14:16
Has thanked: 7 times
Been thanked: 68 times

Re: Interesting tutorial

#22 Post by LE_746F6D617A7A69 »

cuckooflew wrote: There is a problem with it though, it does not remember the "name",

Code: Select all

printf("You Passed!  \nGood work!\n\n", name); 
Actually it does not *print* the name, because there's no appropriate format specifier in the format string: %s
If You would compile the code with -Wall, You'll get a warning: "too many arguments for format" ;)

Correct code should look like this:

Code: Select all

printf("%s: You Passed!\nGood work!\n\n", name); 
A good practice is to enable -Wall -Wextra, at least in test or debug builds.
Bill Gates: "(...) In my case, I went to the garbage cans at the Computer Science Center and I fished out listings of their operating system."
The_full_story and Nothing_have_changed

cuckooflew
Posts: 677
Joined: 2018-05-10 19:34
Location: Some where out west
Been thanked: 1 time

Re: Interesting tutorial

#23 Post by cuckooflew »

Ok, thanks, and that makes sense
Please Read What we expect you have already Done
Search Engines know a lot, and
"If God had wanted computers to work all the time, He wouldn't have invented RESET buttons"
and
Just say NO to help vampires!

cuckooflew
Posts: 677
Joined: 2018-05-10 19:34
Location: Some where out west
Been thanked: 1 time

Re: Interesting tutorial

#24 Post by cuckooflew »

I still needed to change this:
Correct code should look like this:

Code: Select all

printf("%s: You Passed!\nGood work!\n\n", name); 
To this:

Code: Select all

printf(" You Passed!\nGood work!%s \n\n", name); 
to get it to display the text the way I want,...

Code: Select all

Number Incorrect: 0
Number Correct: 1
 You Passed!
Good work!cuckoo
 

parrot$  
But that is something one can experiment with, using the escape sequence \n or \n\n in their c code.
Please Read What we expect you have already Done
Search Engines know a lot, and
"If God had wanted computers to work all the time, He wouldn't have invented RESET buttons"
and
Just say NO to help vampires!

Post Reply