So i am just starting to learn c programming and i decided to practice by making my program speak to the end-user by asking questions and reacting to
them
I first applied if-else statement for the program to react on the age of the person.
then, when i get to the whats your favorite color part with the switch statements it would just close upon pressing any button.
My code:
#include <stdio.h>
#include <conio.h>
#include <dos.h>
#include <ctype.h>
main()
{
char name[25], c;
int a;
clrscr();
printf("Hello, whats your name? \n");
scanf("%s",name);
printf("nice to meet you %s!!!\n");
printf("Whats your age?");
scanf("%d",&a");
{
if((a <= 21) && (a >= 0))
printf("Young!\n");
else if((a <= 100) && (a >= 22))
printf("old!\n");
else
printf("that's not an age!\n");
}
printf("whats your favorite color? \n"); //this is where the program stops//
scanf("%c",&c);
switch(tolower(c)){
case 'r':printf("Fiery!");break;
case 'o':printf("oranggerrr!!");break;
.
. //basically applied the whole rainbow and put some reactions//
.
getch();
return 0;
}
Ok, so I compiled the program online here, making some changes, as the program you have given only compiles in the old Turbo C.
I compiled the following program:
#include <stdio.h>
//#include <conio.h>
//#include <dos.h>
#include <ctype.h>
main()
{
char name[25], c;
int a;
//clrscr();
printf("Hello, whats your name? \n");
scanf("%s",name); //still dont get why it worked without the "&"//
printf("nice to meet you %s!!!\n");
printf("Whats your age?");
scanf("%d",&a);
{
if((a <= 21) && (a >= 0))
printf("Young!\n");
else if((a <= 100) && (a >= 22))
printf("old!\n");
else
printf("that's not an age!\n");
}
printf("whats your favorite color? \n"); //this is where the program stops//
scanf("%c",&c);
switch(c){
case 'r':printf("Fiery!");break;
case 'o':printf("oranggerrr!!");break;
// .
//. //basically applied the whole rainbow and put some reactions//
//.
}
getchar();
return 0;
}
Okay, so when I executed it, I got a segmentation fault, because of this line:
printf ("nice to meet you %s!!!\n");
to
printf ("nice to meet you %s!!!\n", name);
Then everything worked perfectly.
Now your doubt:
scanf ("%s", name);
That works because name is a char array, and the name of an array is equal to the address of the first element in that array. Check this comment. Also, you can see the question.
Improvements:
Change scanf ("%c", &c); to scanf (" %c", &c);
NOTE: I cannot exactly reproduce your problem as I do not have Turbo C. Also, maybe your program crashed because of tolower. Note that tolower return the changed char, so you will have to do: c = tolower (c);
Related
I made a programme to guess user number.
I miss something in my code, but i dont know what.
If i give input b (bigger) or s (lower), its still give me the same result.
Can you tell me please what should i add to Code to work correctly?
#include <stdio.h>
#include <ctype.h>
int main(void)
{
char answer, input;
int untere_grenze=1, upper_limit=999, tipp=0, try1=0;
printf("\n\nThis program tries to guess a number you choose between 1 and 999\n\n");
do
{
try1=0;
do
{
//I think here is my problem
try1+=1;
tipp=untere_grenze+(upper_limit-untere_grenze)/2;
printf("\n%d. try: %d\n", try1, tipp);
upper_limit=tipp-1;
untere_grenze=tipp+1;
//I think here is my problem
do
{
printf("Please enter s (number to be guessed is smaller), b (number to be guessed is larger) or = (guess!):");
scanf(" %c", &input);
input=toupper(input);
} while (input!='S' && input!='B' && input!='=');
} while(input!='=');
printf("\n\nThe computer guessed your number in %d attempts.\n\n", try1);
do
{
printf("Do you want to run the program again (J/N)?\n\n");
scanf(" %c", &answer);
answer=toupper(answer);
} while (answer!='J' && answer!='N');
} while (answer=='J');
return 0;
}
Your code logic is horrible, recommend to rewrite, you can reference mine.
#include <stdio.h>
#include <ctype.h>
#define LOWER_BOUND 1
#define UPPER_BOUND 999
int main (void)
{
int low = LOWER_BOUND, up = UPPER_BOUND, guess, try = 0;
char input;
printf("Program will guess a number between 1 and 999,"
"please input s(smaller), b(bigger), or =(equal)\n");
while (1)
{
if (low <= up)
guess = low + (up - low) / 2;
else {
printf("Program terminate, no more guess value\n");
break;
}
++try;
retry:
printf("guess %d, please input (s/b/=)\n", guess);
scanf(" %c", &input);
if ((input = tolower(input)) == 's') {
up = guess - 1;
} else if (input == 'b')
low = guess + 1;
else if (input == '=')
break;
else
goto retry;
}
printf("Summary: Trial: %d\n", try);
return 0;
}
I don't see your program doing anything with the input.
You should guess lower or higher in the next iteration based on the input.
I started learning C language yesterday and made a simple binary coverter. It works fine on my PC, however, it doesn't work on most online code runners(compilers) except for could9. It looks like the scanf function are not compatible with online IDE in general?
Here's the code.
#include <stdio.h>
#include <string.h>
int main(){
int a;
int b;
int c[255];
char opt[100];
printf("10進数の値を入力してください > ");
scanf("%d", &a);
printf("計算式を表示しますか? y(yes) or n(no) > ");
scanf("%s", opt);
if(strcmp(opt, "y") == 0){
printf("\n計算式: \n");
}
int i = 0;
while(a > 0){
b = a / 2;
c[i] = a % 2;
if(strcmp(opt, "y") == 0){
printf("%d ÷ 2 = %d 余り %d\n", a, b, c[i]);
}
a = b;
i++;
}
printf("\n2進数: ");
int j;
for(j = i-1; j >= 0; j--){
printf("%d", c[j]);
}
printf("\n");
return 0;
}
Any advice will be much appreciated.
My psychic powers tell me you are expecting these online compilers to prompt you for input (like running the program in a terminal window). Unfortunately, this is not the case for most online compilers. Instead, there is a text box somewhere where you type in all of the input that you want in your program's standard input.
For example, take this small program:
#include <stdio.h>
int main()
{
char c;
printf("Gimme a character! ");
fflush(stdout);
scanf(" %c", &c);
printf("You typed in '%c'! Yay! :)\n", c);
}
Compiling and running this in a terminal could produce the following in the window (input is in bold):
Gimme a character! f
You typed in 'f'! Yay! :)
But running this in an online compiler that doesn't prompt you for input could look like this:
Gimme a character! You typed in ' '! Yay! :)
By typing input into the text box provided (where it is depends on the online compiler) you can give input to the program that way.
STDIN:
f
Output:
Gimme a character! You typed in 'f'! Yay! :)
Since, i am kinda very new to C-language I have to build a program which work user-friendly. Like user will give instructions of i need A to Z alphabets and after execute he will get result by using while condition. Is that possible ?.
#include <stdio.h>
int main()
char c;
for(c='A'; c<='Z'; ++c)
printf("%c ",c);
return 0;
}
Like that if I can write it using hard coded values, What if I have to give input From which Alphabet to Alphabet you want result and it give result till user want using while loop.
#include <stdio.h>
int main()
{
char c;
char b;
printf("Enter the first letter\n");
c=getchar();
getchar();
printf("Enter the last letter\n");
b=getchar();
getchar();
for ( ;c<=b; ++c)
{
printf("%c", c);
}
return 0;
}
Hello I am new to C and trying to make a program that asks for the user to input a whole bunch of numbers. I want to loop scanf so that it keeps asking and when the user inputs "0", it stops, reads off the even and odd numbers inputted, and counts them seperatly. Right now I have it to keep asking for new numbers after user presses "Enter" but when i type "0" is just keeps asking for more numbers and doesn't stop. What am I doing wrong? Like I said before, I am very new so baby words are best.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main (void) {
int number_of_integers, sum = 0, i, integer;
char user_name[128];
printf("What is your name?\n");
scanf("%s", user_name);
printf("\nEnter any real numbers followed by ENTER.\n");
while (integer != 0) {
scanf("%s", &integer);
if (integer == 0)
break;
}
printf("%s, the numbers you entered are broken down as follows:\n", user_name);
return 0;
}
As a commenter indicated, we aren't a homework or tutoring service, but I'm doing you a favor by producing an actual working example that I just made up. It's up to you now to tailor it exactly to your needs.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main (void) {
int max=100;
int integer;
int even[max];
int odd[max];
int index=0;
int evencount=0;
int oddcount=0;
char user_name[128];
printf("What is your name?\n");
scanf("%s", user_name);
printf("\nEnter any real numbers followed by ENTER.\n");
while (index < max){
scanf("%d", &integer);
if (integer == 0)
break;
if ((integer % 2) == 0){
evencount++;
even[evencount]=integer;
}else{
oddcount++;
odd[oddcount]=integer;
}
index++;
}
printf("%s, the %d numbers you entered are broken down as follows:\n", user_name,index);
printf("%d odd integer(s):\n",oddcount);
while (oddcount > 0){
printf("%d\n",odd[oddcount]);
oddcount--;
}
printf("%d even integer(s):\n",evencount);
while (evencount > 0){
printf("%d\n",even[evencount]);
evencount--;
}
return 0;
}
I am currently taking a basic C course and I was wondering why my code below doesn't run.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char string[4];
printf("Enter some text\n");
scanf(" %c %c %c", &string[0], &string[1], &string[2]);
printf("You Entered ");
int i;
for (i = 0; i < 4; i++){
printf("%c",string[i]);
}
return 0;
}
Xcode said there is an errr with my scanf line.
I was hoping to type in "a b c d" and expect
a "You entered abcd";
This code should run (albeit with a bug). I suspect you need to configure the Xcode build options correctly.
As for the bug, you have an array of four chars, but you are only scanning for three. Add another %c and &string[3] to your scanf line.
Here's an ideone snippet showing the modified code in action
#include <stdio.h>
int main()
{
char string[4];
int i;
printf("Enter some text\n");
scanf("%c %c %c %c", &string[0], &string[1], &string[2], &string[3]);
printf("You Entered ");
for (i = 0; i < 4; i++){
printf("%c", string[i]);
}
return 0;
}
This compiles just fine on the Mac command line (assuming the source is in "test.c")
$ cc -g -Wall -o test test.c
./test
Enter some text
a b c d
You Entered abcd
Also note that this particular snippet requires only stdio.h (man scanf and man printf will tell you which header to use).
How about executing this code?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char strin[10];
printf("Enter some text\n");
scanf("%s", strin);
printf("You Entered %s",strin);
return 0;
}
The following code gives you :
Enter some text
abcd
You Entered abcd