cuckooflew wrote:That is a good one, for bash script
cuckooflew wrote:I was hoping for C++
#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;
}
parrot$ ./name
Hello. What's your name?
cuckoo
Hi there, cuckoo
parrot$
Bash has a POSIX mode too - but I don't like it - it's better to live on the edgeHead_on_a_Stick wrote:cuckooflew wrote:That is a good one, for bash script
Thanks but I don't like bash. POSIX sh ftw!
Head_on_a_Stick wrote:Don't bother with C++, it's rubbish: http://harmful.cat-v.org/software/c++/
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
#!/bin/sh
#!/bin/sh
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
#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;
}
printf("You Passed! \nGood work!\n\n", name);
math2 compile instruction
gcc math2.c -lpthread
to run it: ./a.out
gcc -o hello hello.c
use ./hello to run
-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.
gcc -o math math.c
Actually it does not *print* the name, because there's no appropriate format specifier in the format string: %scuckooflew 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);
printf("%s: You Passed!\nGood work!\n\n", name);
Correct code should look like this:
- Code: Select all
printf("%s: You Passed!\nGood work!\n\n", name);
printf(" You Passed!\nGood work!%s \n\n", name);
Number Incorrect: 0
Number Correct: 1
You Passed!
Good work!cuckoo
parrot$
Users browsing this forum: No registered users and 4 guests