Interesting tutorial

Need help with C, C++, perl, python, etc?

Re: Interesting tutorial

Postby cuckooflew » 2020-08-16 18:11

That is a good one, for bash script, I was hoping for C (edited, I meant to say C) , but I did not specify that, so yes that is one way to do it,... I still have not tried search anything yet, I am sure there something in some tutorial,... but I need to take a nap just now..my medicine makes me sleepy.
Last edited by cuckooflew on 2020-08-16 18:57, edited 1 time in total.
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: 683
Joined: 2018-05-10 19:34
Location: Some where out west

Re: Interesting tutorial

Postby Head_on_a_Stick » 2020-08-16 18:14

cuckooflew wrote:That is a good one, for bash script

Thanks but I don't like bash. POSIX sh ftw!

cuckooflew wrote:I was hoping for C++

Don't bother with C++, it's rubbish: http://harmful.cat-v.org/software/c++/

Plain C is better. Perhaps also look at Go or Rust. I can't use either though, unfortunately :(
Black Lives Matter

Debian buster-backports ISO image: for new hardware support
User avatar
Head_on_a_Stick
 
Posts: 12640
Joined: 2014-06-01 17:46
Location: /dev/chair

Re: Interesting tutorial

Postby cuckooflew » 2020-08-16 18:56

Thanks
Found one, and it is C , not C++: (I meant C, don't know why I said c++ )
Code: Select all
#include <stdio.h>

int main(void)
{
    char name[20];
    printf("Hello. What's your name?\n");
    //scanf("%s", &name);  - deprecated
    fgets(name,20,stdin);
    printf("Hi there, %s", name);
    return 0;
}

Code: Select all
parrot$ ./name             
Hello. What's your name?
cuckoo
Hi there, cuckoo
parrot$ 
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: 683
Joined: 2018-05-10 19:34
Location: Some where out west

Re: Interesting tutorial

Postby LE_746F6D617A7A69 » 2020-08-16 19:19

Head_on_a_Stick wrote:
cuckooflew wrote:That is a good one, for bash script

Thanks but I don't like bash. POSIX sh ftw!
Bash has a POSIX mode too - but I don't like it - it's better to live on the edge :mrgreen:

Head_on_a_Stick wrote:Don't bother with C++, it's rubbish: http://harmful.cat-v.org/software/c++/

Oh no! C++XX is just as modern as Java - it allows the programmer to focus on the problem, instead on the code...

...but hmm, aren't the programmers expected to write a code?
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
LE_746F6D617A7A69
 
Posts: 389
Joined: 2020-05-03 14:16

Re: Interesting tutorial

Postby cuckooflew » 2020-08-16 21:00

Head_on_a_Stick wrote:
cuckooflew wrote:That is a good one, for bash script

Thanks but I don't like bash. POSIX sh ftw!

cuckooflew wrote:I was hoping for C++

Don't bother with C++, it's rubbish: http://harmful.cat-v.org/software/c++/

Plain C is better. Perhaps also look at Go or Rust. I can't use either though, unfortunately :(

Your right, I just did not notice:
Code: Select all
#!/bin/sh

With shell scripts I don't use the
Code: Select all
#!/bin/sh

Is that bad, ? I just use this part EG:
Code: Select all
echo 'Please enter your name'
IFS= read -r reply
echo "Hello $reply, welcome. Please hit enter to exit."
while true; do
   IFS= read -r key
   case "$key" in
      "") echo 'Thank you'
         exit 0
         ;;
      *) echo 'Please hit enter to exit...'
         ;;
   esac
done
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: 683
Joined: 2018-05-10 19:34
Location: Some where out west

Re: Interesting tutorial

Postby cuckooflew » 2020-08-17 00:34

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!
cuckooflew
 
Posts: 683
Joined: 2018-05-10 19:34
Location: Some where out west

Re: Interesting tutorial

Postby LE_746F6D617A7A69 » 2020-08-18 09:43

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
LE_746F6D617A7A69
 
Posts: 389
Joined: 2020-05-03 14:16

Re: Interesting tutorial

Postby cuckooflew » 2020-08-18 13:15

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: 683
Joined: 2018-05-10 19:34
Location: Some where out west

Re: Interesting tutorial

Postby cuckooflew » 2020-08-24 10:08

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!
cuckooflew
 
Posts: 683
Joined: 2018-05-10 19:34
Location: Some where out west

Previous

Return to Programming

Who is online

Users browsing this forum: No registered users and 3 guests

fashionable