0

So I have been trying to read input string and then print it by not using neither scanf() nor printf() but getchar() and putchar() instead. It seems like the program is stuck in the loop, I'm not able to spot an error.

#include <stdio.h>

void getstring(char *c)
{
char inS[100];
char n;
int i = 0;  
while ((n = getchar()) != '\n') {
    inS[i] = n;
    i++;        
}
inS[i] = '\0';
i = 0;
while (inS[i] != '\0') {
    putchar(inS[i]);
    i++;
}
}
main()
{
char *prompt;
prompt = "Enter a sentence: \n";
getstring(&prompt);
printf("%s", prompt);

}
Walle
  • 197
  • 1
  • 10

6 Answers6

1

Maybe you are not adding \n in your stdin? Executing your code was successful. Also you are not modifying passed char *c, why? And to modify a pointer you should pass a pointer to a pointer (How do I modify a pointer that has been passed into a function in C?)

Community
  • 1
  • 1
Nikita Lapkov
  • 121
  • 1
  • 6
1

Your code has some problems. Here is the corrected code:

#include <stdio.h>

void getstring(void) /* You don't use the passed argument. So, I've removed it */
{
    char inS[100];
    /* `char n;` getchar() returns an int, not a char */
    int n;
    int i = 0;  
    while (i < 99 && (n = getchar()) != '\n' && n != EOF)  /* Added condition for preventing a buffer overrun and also for EOF */
    {
        inS[i] = n;
        i++;        
    }
    inS[i] = '\0';
    putchar('\n'); /* For seperating input and output in the console */
    i = 0;
    while (inS[i] != '\0')
    {
        putchar(inS[i]);
        i++;
    }
    putchar('\n'); /* For cleanliness and flushing of stdout */
}
int main(void) /* Standard signature of main */
{
    char *prompt;
    prompt = "Enter a sentence: \n";
    printf("%s", prompt); /* Interchanged these two lines */
    getstring(); /* Nothing to pass to the function */
    return 0; /* End main with a return code of 0 */
}

Note: The loop for input will end when either

  • A \n(Enter) has been encountered.
  • An EOF has been encountered.
  • 99 characters were read from stdin.
Spikatrix
  • 20,225
  • 7
  • 37
  • 83
0

First you can make all the elements of the array to NULL.

 char inS[100] = {"\0",};

You can use gets() to get the string from the console. some thing as below.

your code

while ((n = getchar()) != '\n') {
    inS[i] = n;
    i++;        
}

Modify it as:

gets(inS);

Then you can do the other operation what ever you want.

Sudipta Kumar Sahoo
  • 1,049
  • 8
  • 16
  • 1
    `gets()` is no longer part of the standard C library for the past 5 years. – chux - Reinstate Monica Mar 06 '16 at 05:08
  • @chux, Sorry to say you, but that exists. After your comment I checked and verified using gets(). Which is working absolutely fine. for your info, Please check the any kernel source code those are implemented in C, widely uses gets(). – Sudipta Kumar Sahoo Mar 06 '16 at 05:15
  • 1
    C11 removed `gets()`. "removed the `gets` function (``)". Perhaps you are working with a non up-to-date compiler or codebase? – chux - Reinstate Monica Mar 06 '16 at 05:18
  • @chux Why do you assumes it would be C11. Any way the question was not tagged with C11. – Sudipta Kumar Sahoo Mar 06 '16 at 05:21
  • My comments made no assumption about the code being C11, C99 or earlier. Simple pointed out "gets() is no longer part of the standard C library for the past 5 years" which is when C11 was made the standard. – chux - Reinstate Monica Mar 06 '16 at 05:24
  • I sorry to say. I think you can go through windows or linux kernel source code for a batter understanding. These people are still using them. Any way it is again on to you how and what you want to use :) – Sudipta Kumar Sahoo Mar 06 '16 at 05:28
  • [simple replacement for gets()](http://stackoverflow.com/questions/34031129/since-the-standard-c-committee-did-not-standardize-a-simple-replacement-for-gets) addresses the replacement issue. Since it is not available with current C compliers, new source code should not use it. – chux - Reinstate Monica Mar 06 '16 at 05:34
0

getchar replaces \n character with \0 whenever it sees a newline character. So n will never be \n. You should try getc or fgetc.

0x0001
  • 525
  • 4
  • 12
0

You are not printing a new line. After putchar(ch) you should use putchar('\n') to print a new line

Vlad
  • 115
  • 7
0
#include <stdio.h>
void getstring(char *c)
{
char n;
int i = 0;
while ((n = getchar()) != '\n')
{
 *c=n;
  c++;
}
c = '\0';
main()
{
char inS[100];
char *prompt=inS;
getstring(prompt);
printf("%s", prompt);
}

strong text

In main() function only pointer is declared but it is not assigned to any symbol in other words pointer declared but it does not refer to any memory location. That problem is solved in this solution

  • Add those description to answer, not in comment, So that OP will find it helpful. That's not for me,. I was from review – Shree Krishna Mar 06 '16 at 08:33