Hey I am beginner in programming i need help to solve this problem
I want that i input some character and i want to print it..
#include<stdio.h>
int main()
{
char a[50];
printf("Enter character...");
scanf("%s",&a[50]);
printf("You entered is %s",a[50]);
return 0;
}
From the above code I am not getting any output i also try using loop but not get any result please help me what is correct program so that if i enter any string i print on the screen?
So here's your issue.
You are defining an array of type char with a length of 50. You then read from stdin a string, and then store it at the address of the 50th element. So what will happen is you are storing the string "out of bounds" and you may get a crash, or may not.
Either way, something very bad is happening. You are writing data to an area of memory that you should not be.
So what you want to do is write that data to the address of the 0th index of the array.
You do that by using &a[0] or, for simplicity's sake: a. Both mean the same thing.
At the end of the day, what you want is this:
#include<stdio.h>
int main()
{
char a[50];
printf("Enter character...");
scanf("%s",a);
printf("You entered is %s",a);
return 0;
}
I understand you are a beginner and are learning basic concepts, but keep in mind, this code is very unsafe. Because if someone types in a length of characters longer than 50, you are back in the same boat you were before.
Quoting kaylum's comment, "It would be beneficial to go through a basic C book or tutorial before proceeding further."
Now about your issue, change these lines:
scanf("%s",&a[50]);
printf("You entered is %s",a[50]);
to
scanf("%50s", a);
printf("You entered \"%s\".\n", a);
If you just want to print what's inputted by the user then you could use buffer:
#include <stdio.h>
int main(void)
{
char c;
printf("Enter the string(~ to exit)\n");
while((c = getchar()) != '~')
putchar(c);
return 0;
}
Output:
Enter the string(~ to exit)
This is a test program // press enter
This is a test program // same output
~ // exit
Related
When I enter a string in the code below, the program doesn't move on. It just allows me to keep typing and pressing enter with no effect. Why does this happen and how can I fix it.
#include <stdio.h>
main() {
char str[20];
int aaa = 0;
int exit;
printf("Enter anything: ");
scanf("%s", str);
while(aaa == 0) {
if(str[3] == 'a') {
aaa++; }
else {
scanf("%d", &exit);
if(exit == 3) {
aaa++; } } }
printf("%s\n", str);
}
Log:
Enter anything: 2/3/4444
Now
it
just
lets
me
keep
on
typing
Edit: I solved it and I’m a bit embarrassed at how simple it was. I know people have been trying to explain this to me but in my own words this is what was happening: as the condition to enter the while loop was being met the program would enter the while loop. However, unless the input entered for the scanf satisfied one of the conditions in the loop, the program had no way of leaving the loop and therefore, it would get stuck. Basically I was simply missing an else statement which solved this problem.
After a string whose fourth character is not an a, your program reads an integer. It will never attempt to read anything but an integer after that first read. So you must not enter anything but an integer after the first string.
If you want your program to handle a non-integer after the string, you need to add code to do that. You currently have none.
I apologize if a similar question was answered before but since I'm a first grade programming student I don't really catch up with this C language and so couldn't find my answer.
The result is that I want the user to input a word for ex.: "aleksandras" and after then input a letter say "g" to replace all "a" letters in that word.
I've tried 'debugging' but no result since to me everything looks like it should work as I want it to (you will notice a lot of 'printf's that's basically how I did it).
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char zodis[20];
printf("Ivesk 20 raidziu zodi: ");
scanf("%s", zodis);
int zodzioIlgis = strlen(zodis);
char naujasSim[1];
printf("Ivesk raide, kuri pakeis 'a' raides: ");
scanf("%s", naujasSim);
printf("Ivedei: %s \n", naujasSim);
for(int i = 0; i <= zodzioIlgis; i++){
if(zodis[i] == 'a'){
zodis[i] = *naujasSim;
printf("'if' ivyko \n");
}
printf("prasibegau\n");
}
printf("Pakoreguotas zodis: %s", zodis);
}
I did notice debugging that when say user inputs "aleksandras" the loop ignores the first letter "a" which is supposed to be noticed at the very least but printf("'if' ivyko \n"); doesn't happen at that point. Also it seems that loop doesn't even start if I type int zodzioIlgis = strlen(zodis); right before the loop because printf("prasibegau\n"); doesn't print. Why so?
To summarize my current code doesn't replace the letters I want it to better yet it just 'deletes' the word from the var which then prints nothing basically.
I would greatly appreciate if someone could explain why isn't the replacement working as well as regarding my debugging questions!
Thanks in advance!
I am writing a code to see whether or not the user's input is equivalent to an already stated string. The program loops until the input is the same as the string, using the strcmp function, but for some reason the program does not compare the strings and therefore the loop malfunctions. The code is below:
int main()
{
char passcode[3]="ZZZ";
char input[3];
int check;
while(check!=0)
{
printf("What is the password?\n");
gets(input);
check=strcmp(passcode, input);
}
printf("You crack the pass code!");
return 0;
}
The main problem is here:
char passcode[3]="ZZZ";
char input[3];
A string in C consists of a sequence of characters followed by a null byte. passcode is not large enough to hold the null byte for the string it's initialized with. So when you try to use it as a string by passing it to strcmp it reads past the end of the array. Doing so invokes undefined behavior.
Similarly, input isn't big enough to hold a string big enough to compare against.
You're also not initializing check, so the first time you enter the loop it's value is unknown.
Another problem is the use of gets. This function is dangerous because it does not check if the string the user entered can fit into the given buffer. If is is too big, this again invokes undefined behavior.
Make your arrays larger to hold the user's input as well as the target string, and use fgets instead of gets. You should also change the while loop to do..while since you need to enter the loop at least once.
#include <stdio.h>
int main()
{
char passcode[]="ZZZ"; // array is automatically sized
char input[50];
int check;
do {
printf("What is the password?\n");
fgets(input, sizeof(input), stdin);
check=strcmp(passcode, input);
} while (check!=0);
printf("You crack the pass code!");
return 0;
}
The suggested code above does not recognize the input. It likely wont work and will stuck within the while loop. I would suggest to make it much easier using scanf for the input and then compare the string as you did with strcmp. If input is correct then let in and break out of the while loop. Try this:
#include <stdio.h>
int main()
{
char input[3];
printf ("\nHit the pass code!\npass code: ");
while (input != "ZZZ") {
scanf ("%s",&input);
if (strcmp(input, "ZZZ") == 0){
printf ("\nYou crack the pass code!!\n\n");
break;
} else {
printf ("Wroooong!\n pass code: ");
}
}
return 0;
}
I see what is going on. Your input string is only three three bytes and you are reading using the unsafe gets. The gets is putting the input of ZZZ into the input variable as expected but it is putting the terminating null in the first byte of passcode.
Change the size of your input buffer to 999 and things will work a lot better.
I'm running a while loop so the user can constantly enter expressions, until they indicate they want to quit the program. I'm using strcmp() to compare two strings so as soon as they enter quit the program will stop. But the program keeps going, any Ideas?
#include <stdio.h>
#include <string.h>
int main()
{
int min12=0;
char opper;
int x=0;
int min13;
char *Repeatprog="cont";
char *Repeatprog1="quit";
while (strcmp(Repeatprog,Repeatprog1))
{
printf("enter the integer number \n");
scanf( "%d %c %d", &min12, &opper, &min13);
printf("%d %c %d\n", min12, opper, min13);
printf("Type the word quit to end program\n");
scanf("%s", Repeatprog);
}
printf("Good Bye");
return 0;
}
Remember always that an Array is a Pointer to the first object of the array.
And secondly, in your call to scanf() you only read a character. Not a whole string (represented by %s in C)
So in conclusion, your call to scanf() shouldn't have a pointer and should have a string instead of a character.
scanf("%s", Repeatprog);
or simply
gets (Repeatprog);
EDIT :
As the commenter #EOF said, gets() is not a good idea since it can lead to Undefined Behaviour. That's because the program can read more characters than it should have and lead to overflow, thus it isn't secure.
So I recommend using char *fgets(char *str, int n, FILE *stream)
Note:
Also, your code is using string literals. So if you make any attempt to change the content of the char pointer then it will lead to Undefined Behaviour.
For this note, please thank the guys below me [comments]. I made a huge mistake and I'm sorry.
I am trying to create a program that prints a random number whenever the user enters "roll", and allows the user to enter "1" if the random number is greater than or equal to 3.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
int main(void)
{
int randomnumber;
char diceinput;
int slotnumber;
char lettertable[7];
char *character;
lettertable[1] = 'l';
lettertable[2] = 'r';
lettertable[3] = 't';
lettertable[4] = 'e';
lettertable[5] = 't';
lettertable[6] = 'e';
character = &lettertable[1];
printf("Enter 'roll' to roll the dice. Enter 'exit' to quit.\n");
scanf("%s", &diceinput);
if (strcmp(&diceinput, "exit")==0) {
printf("Now quitting. Thank you, and please play again!\n");
}
if (strcmp(&diceinput, "roll")==0) {
srand((unsigned)time(NULL));
randomnumber = rand() % 6 + 1;
printf("%d\n", randomnumber);
if (randomnumber >= 3) {
printf("Enter 1 to get the corresponding letter from table.\n");
scanf("%d", &slotnumber);
if (slotnumber == 1) {
printf("%s", character);
}
}
}
}
After the user enters "1" the program is supposed to get the letter stored in the letter table[1], an element in the array lettertable. However, when I run the program and enter "1", instead of getting the letter "l", the output is a weird phrase: "lrtete" with an upside down question mark. Can somebody please help me? Thank you.
Please note that the code shown above is only a revelant section of the unfinished program.
Try this
if (slotnumber == 1)
{
printf("%c", *character);
}
Also as pointed by Jonathan Leffler your code does not quit for if (strcmp(&diceinput, "exit")==0). You should use exit(0) to quit;
if (strcmp(&diceinput, "exit")==0)
{
printf("Now quitting. Thank you, and please play again!\n");
exit(0);
}
if you're trying to output one letter try replacing printf("%s" , character) with printf("%c" , *character)
Use %c as in printf("%c", *character); instead of %s in printf("%s", character);
You have a few logical mistakes in your code:
1) char diceinput; and then scanf("%s", &diceinput);, this is a big mistake, your program may crash as you don't have sufficient memory allocated (neither statically nor dynamically) for the input string. Use char diceinput[5]; and scanf("%s", diceinput);
2) You have started indexing from 1. (Pardon me if its intentional)
3) You are using printf("%s", character);, I will say its your luck that you are getting "lrtete" as output, i.e. it may not stop at ..te and print some garbage characters after, because there is no explicit \0 character at the end of character array.
The printf function will print what it finds at the location provided and will stop printing when it finds a binary zero. Your problem is here:
printf("%s", character);
Since the second element of the array is not zero it will keep printing until it happens to find a zero. In your case you were lucky and it found one before printing lots of garbage.