Why in this code its doing an infinite loop without prompting again the user
#include <stdio.h>
#include <string.h>
#include <ctype.h>
void to_rot13(char* value);
int main(){
char word[1024];
printf("C ROT13\nSome data to code or decode\n");
while (1){
printf(": ");
scanf("%[^\n]s", word);
to_rot13(&word);
printf(": %s\n", word);
}
return 0;
}
void to_rot13(char* value){
unsigned int x;
for (x = 0; value[x] != '\0'; x++){
if ((value[x] < 'A') || (value[x] > 'Z' && value[x] < 'a') || (value[x] > 'z')){}
else if (tolower(value[x]) <= 'm'){value[x] = value[x] + 13;}
else{value[x] = value[x] - 13;}
}
}
I would like to prompt again the user
I can't be more precice.
scanf("%[^\n]s", word);
leaves the newline in the input buffer, so the next scanf immediately returns without reading any further input, since the first char left in the buffer is a newline. You need to remove the newline from the input buffer.
int c;
do {
c = getchar();
}while(c != '\n' && c != EOF);
if (c == EOF) {
exit(EXIT_FAILURE); // input borked
}
Also, heed the compiler's warning and pass word instead of &word to to_rot13.
You're sending &word to to_rot13 (you should be sending word or &word[0])
Related
Write a program that reads lines from the standard input. Each line is printed on the standard output preceded by its line number. Try to write the program so that it has no built in limit on how long a line it can handle.
#include <stdio.h>
int main()
{
int ch;
int pos = 1;
printf("Enter the line :\n");
while ((ch = getchar()) != EOF)
{
if (pos == 1)
{
printf("%d\t", pos);
pos++;
}
putchar(ch);
if (ch == '\n')
printf("%d\t", pos++);
}
}
OP's code is almost there.
Keep track of line number and column position.
User input might not end with a '\n'. Better to increment and print the line number when in column position 0 and data has arrived.
To avoid numeric limits, code could use a wider type than int.
#include <stdio.h>
int main(void) {
int ch;
long long line_count = 0;
long long column_position = 0;
printf("Enter the line :\n");
while ((ch = getchar()) != EOF) {
if (column_position == 0) {
printf("%lld\t", ++line_count);
}
column_position++;
putchar(ch);
if (ch == '\n') {
column_position = 0;
}
}
fflush(stdout); // insure any last line without a \n is printed before quitting
}
Is very easy (and faster) using fgets.
Just print the line, then search for a trailing newline (strchr can help) and if you find it, print the number of line.
#include <stdio.h>
#include <string.h>
int main(void)
{
char str[2048];
int ln = 0;
printf("%5d) ", ++ln);
while (fgets(str, sizeof str, stdin)) {
printf("%s", str);
if (strchr(str, '\n')) {
printf("%5d) ", ++ln);
}
}
puts("EOF");
return 0;
}
I've encountered a problem when validating a single-char scanf input in C and I cannot find an existing solution that works...
The scenario is: a method is taking a single letter 'char' type input and then validating this input, if the criteria is not met, then pops an error message and re-enter, otherwise return this character value.
my code is:
char GetStuff(void)
{
char c;
scanf("%c", &c);
while(c != 'A' || c != 'P')
{
printf("invalid input, enter again (A for AM or P for PM): ");
scanf ("%c", &dtChar);
}
return c;
}
however, i got the infinite loop of error message no matter what input I type in. I read some other posts and guess it's the problem that %c specifier does no automatically get rid of the newline when I hit enter, and so far I have tried:
putting a white space before/after %c like:
scanf(" %c", &c);
write a separate method or include in this GetStuff method to clean the newline like:
void cleanBuffer(){
int n;
while((n = getchar()) != EOF && n != '\n' );
}
Can anyone help me with this problem please? Thank you in advance.
Please consider the following snippet:
#include <stdio.h>
#include <ctype.h>
char GetStuff(void)
{
char c;
do {
printf("Please enter A for AM or P for PM: ");
scanf ("%c", &c);
// clean input buffer (till the end of line)
while(getchar()!='\n');
} while(toupper(c) != 'A' && toupper(c) != 'P');
return c;
}
int main(void)
{
printf("Your input is'%c'\n", GetStuff());
return 0;
}
Note the points:
condition while(c != 'A' || c != 'P') will be always true (just because one character cannot be 'A' and 'P' at the same time), so use while(c != 'A' && c != 'P') instead
No need for two scanf if you use do..while loop
After entering a char with scanf it is recommended to clean all characters from buffer, e.g. with while(getchar()!='\n'); (this will clean all input including incorrect and redundant characters)
use toupper to avoid making 4 comparison (actually single c=toupper(c) inside loop can minimize your while as while(c != 'A' && c != 'P') )
UPDATE:
To add message "Invalid input" and adding some other useful improvement subjected befor... new code is as:
#include <stdio.h>
#include <ctype.h>
void CleanBuffer(){
int n;
while((n = getchar()) != EOF && n != '\n' );
}
char GetStuff(void)
{
char c;
do {
printf("Please enter A for AM or P for PM: ");
scanf (" %c", &c);
c = toupper(c); // here letter become uppercase
CleanBuffer();
} while( (c != 'A' && c != 'P')?printf("Invalid input! "):0 );
return c;
}
int main(void)
{
printf("You have entered: %c\n", GetStuff());
return 0;
}
Note: function will return 'A' or 'P' in uppercase, so if this is not needed change the code as in example before update (use two toupper and do not change c after scanf). Also you can use tolower as an option (of course with comparing to 'a' and 'p').
#include <stdio.h>
char GetStuff(void) {
char c;
scanf("%c", &c);
getchar();
while ((c != 'A') && (c != 'a') && (c != 'P') && (c != 'p')) {
printf("invalid input, enter again (A for AM or P for PM): ");
scanf ("%c", &c);
getchar();
}
return c;
}
int main(void) {
printf("Calling GetStuff()...\n");
char x = GetStuff();
printf("User entered %c\n", x);
return 0;
}
You are using while (c != 'A' || c != 'P') as your loop conditional, but this will always return true. What you meant to use is the && "and" operator, instead of the || "or" operator.
Also, call getchar() after your scanf statements, to capture the newline. This should work the way you want it to.
Inside loop you are taking input in dtChar but your loop condition checks variable c which is not updated in the loop, that is causing infinite loop
Also you would change your condition
while(c != 'A' || c != 'P')
to
while(c != 'A' && c != 'P')
If you want user to enter either 'A' or 'P'
Another possible solution. As others mentioned the condition was to be done with &&. Anyway the big problem is how to remove what's left on the console input line. Since the console works by lines, we remove everything up to the next '\n'. If the user already left something on the input line before calling GetStuff(), it would be useful to add a call to SkipRestOfTheLine() before the while loop.
In general I suggest to start with a while(1) loop, before making it nicer (such as in the cleanBuffer() you posted).
#include <stdlib.h>
#include <stdio.h>
void SkipRestOfTheLine(void)
{
while (1) {
int c = fgetc(stdin);
if (c == EOF || c == '\n')
break;
}
}
char GetStuff(void)
{
while (1) {
int c = fgetc(stdin);
if (c == EOF)
exit(EXIT_FAILURE); // Deal with this case in an appropriate way
if (c == 'A' || c == 'P')
return c;
printf("invalid input, enter again (A for AM or P for PM): ");
SkipRestOfTheLine();
}
}
int main(void)
{
char c = GetStuff();
return 0;
}
try this,
char GetStuff(void)
{
char c;
scanf("%c", &c);
while (((c != 'A') || (c != 'a')) && ((c != 'P') || (c != 'p'))==1)
{
printf("invalid input, enter again (A for AM or P for PM): ");
scanf ("%c", &dtChar);
}
return c;
}
I hope this works, some time because of not given proper bracket it is stuck in the loop.
#include <stdio.h>
int main(){
char c;
do{
printf("invalid input, enter again (A for AM or P for PM): ");
scanf ("%s", &c);
}while ((c != 'A') && (c != 'P'));
return 0;
}
I'm trying to write a code which will replace character in the string user selects with character he/she does. Eg string london if user picks o and a then the output should be landan.
Here is the code:
#include <stdio.h>
#include <string.h>
#define MAXLEN 100
int function2(char str[], char drop, char sub) {
int i = 0; int num = 0;
while (str != NULL) {
if (str[i] == drop) {
str[i] = sub;
num++;
}
i++;
}
return num;
}
int main() {
char d, s;
char my_string[MAXLEN];
printf("Input your string\n");
scanf("%s", &my_string);
printf("Input character you want to drop\n");
scanf(" %c", &d);
printf("Now character you want to substitute\n");
scanf(" %c", &s);
function2(my_string, d, s);
printf("The string is %s\n", my_string);
return EXIT_SUCCESS;
}
It works up until the point where you actually print the altered string. All I get is Segmentation fault (core dumped). Note that code for function was not mine (I found it on some website, so owner of the original code for function2- Thank you in advance). Any help would be appreciated!
First of all, you should avoid using scanf. If you're interested for the reason and alternatives click here.
But back to your problem
while(str != NULL)
is an infinite loop, because the pointer won't become NULL
while(str[i] != '\0')
should do the trick. It checks each time if you've already arrived at the end of the string.
if (str != null){
while(str[i] != '\0'){
if (str[i] == drop){
str[i] = sub;
num++;
}
i++;
}
}
str is a char array, with str != NULL, you check that the array point to a valid memory address.
With while loop and i++, you loop all characters in array, because the string ends with '\0', you need to stop the loop with while(str[i] != '\0').
Your function runs an infinite loop because str never become NULL, but since i is incremented, str[i] will eventually access memory beyond the end of the string and at some point invalid memory causing a Segmentation fault.
Note also that it is not simple to tell scanf() the maximum number of characters to read into my_string. Using fgets() is much safer and allows a whole phrase to be substituted.
Here is a corrected version:
#include <stdio.h>
#include <stdlib.h>
#define MAXLEN 100
int function2(char str[], char drop, char sub) {
int num = 0;
for (int i = 0; str[i] != '\0'; i++) {
if (str[i] == drop) {
str[i] = sub;
num++;
}
}
return num;
}
int main(void) {
char d, s;
char my_string[MAXLEN];
printf("Input your string\n");
if (!fgets(my_string, MAXLEN, stdin))
return EXIT_FAILURE;
printf("Input character you want to drop\n");
if (scanf(" %c", &d) != 1)
return EXIT_FAILURE;
printf("Now character you want to substitute\n");
if (scanf(" %c", &s) != 1)
return EXIT_FAILURE;
function2(my_string, d, s);
printf("The modified string is %s", my_string);
return EXIT_SUCCESS;
}
I'm trying to program a loop that counts characters until it receives a certain sentinel value. The sentinel value is supposed to be a #, but I've also tried a 0 and a Z and had the same response.
When it compiles, I receive "warning: comparison between pointer and integer" for lines 16 (the line that calls the sentinel.)
If I don't define the sentinel, but instead rely on logical operators in the while statement, then I receive no error, but have an endless loop.
Thanks!
#include <stdio.h>
int main()
{
#define SENTINEL '#'
char ch;
int chcount;
printf("Enter your text, terminate with a #:");
scanf("%s", &ch);
chcount = 0;
while (ch != SENTINEL)
{
if ((ch >= 'A') && (ch <= 'Z'))
{
chcount = chcount +1;
printf("You have entered %d characters", chcount);
}
}
return(0)
}
With the %s format specifier, scanf expects the address of a char buffer, where the string you type will be copied.
And you gave the address &ch of a single char, which is obviously not enough to contain a "word" from input with its terminating null character.
Moreover, your loop reads no input from the user. Thus the endless loop.
This is because the way you use scanf(), with %s format specifier you are writing to a char*, not the char ch (as you've declared). In order to write to a single char variable, you should use a %c format specifier.
To fix this you should either use f.e. getchar() instead of scanf() or use scanf() (and change ch to char* then) but iterate over scanned string to check whether there is #.
I would recommend the first solution.
The while loop never ends so I changed your while loop.
I tried to change your program to:
#include <stdio.h>
#define SENTINEL '#'
int main()
{
char ch;
int chcount;
printf("Enter your text, terminate with a #:");
chcount = 0;
while ((ch = getchar()) != SENTINEL)
{
if ((ch >= 'A') && (ch <= 'Z'))
{
chcount = chcount + 1;
printf("You have entered %d characters\n", chcount);
}
}
return(0);
}
Some issues I found with your code:
scanf("%s", &ch);
It should be
scanf("%c", &ch);
Next, semicolon missing here: return(0);
However, since your aim is:
I'm trying to program a loop that counts characters until it receives a certain sentinel value. The sentinel value is supposed to be a #
I suggest moving your scanf() inside while loop:
#include <stdio.h>
int main()
{
#define SENTINEL '#'
char ch='0';
int chcount;
printf("Enter your text, terminate with a #:");
chcount = 0;
int i=0;
while (ch != SENTINEL)
{ scanf("%c", &ch);
if ((ch >= 'A') && (ch <= 'Z'))
{
chcount = chcount +1;
printf("You have entered %d characters", chcount);
i++;
}
}
return(0);
}
here is a working version of the posted code.
It contains numerous corrections.
Corrections include consistent/usable indentation and logic corrections
Note: not all implementations have the getline() function
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
int main( void )
{
int sentinelFound = 0;
#define SENTINEL '#'
char* line = NULL;
size_t lineLen = 0;
printf("Enter your text, terminate with a #:");
int chcount;
getline(&line, &lineLen, stdin );
size_t i;
for( i=0; i<lineLen; i++)
{
if( SENTINEL == line[i] )
{
sentinelFound = 1;
break;
}
if ((line[i] >= 'A') && (line[i] <= 'Z')) // only count capital letters
{
chcount = chcount +1;
}
}
free( line );
if( !sentinelFound )
printf( "You did not enter the sentinel character!" );
else
printf("You have entered %d capital characters\n", chcount);
return(0);
} // end function: main
I have two problems writing my code. The first problem I have is getting my getchar() to work if the user enters no text and just hits enter. I need to print an error if they do so and prompt the user to reenter the text in a loop until they do enter text. Is there any way to do so because everything I have tried has failed.
Here is the code I have for that section:
printf("Enter a text message: ");
while((c=getchar()) != '\n' && c != EOF)
{
text[i]= c;
i++;
}
I am new to C so I am limited on ideas to fix my dilemma. As you can see I am setting the input equal to an array. This leads to my second problem, I need to limit the input to no more than 100 characters. But, instead of giving the user an error I need to just chop off the extra characters and just read the first 100.
The simplest solution to your problem is to use fgets. We can give limit to the input so that it doesn't read the extra characters after the given limit.
Refer this sample code. Here I am printing the string if the user is not pressing Enter key:
#include <stdio.h>
int main()
{
char str[100];
fgets(str, 100, stdin);
if(str[0] != '\n')
{
puts(str);
}
return 0;
}
#include <stdio.h>
#define MAXSIZE 100
int main() {
char text[MAXSIZE+1]; // one extra for terminating null character
int i = 0;
int c;
while (1) {
printf("Enter a text message: ");
i = 0;
while ((c = getchar()) != '\n' && c != '\r' && c != EOF) {
if (i < MAXSIZE) {
text[i]= c;
i++;
}
}
if (i > 0 || c == EOF)
break;
printf("Empty string not allowed.\n");
}
text[i] = '\0';
printf("You entered: %s\n", text);
return 0;
}
Test code to detect non-compliant system:
#include <stdio.h>
int main() {
int c;
printf("Just hit enter: ");
c = getchar();
if (c == '\r')
printf("\\r detected!!!\n");
else if (c == '\n')
printf("\\n detected.\n");
else
printf("Yikes!!!\n");
return 0;
}
First of all getchar() can take only one character an input. It cannot take more than one character.
char c;
int total_characters_entered = 0;
do
{
printf ("Enter a text message: ");
c = getchar();
if (c != '\n')
{
total_characters_entered++;
}
} while (total_characters_entered <= 100);
I have written some code that will iterate in while loop until user has entered 100 characters excluding "Simple Enter without any text"
Please let me know if it does not satisfy your requirement. We will work on that.