-4

When running the program and entering the text, it will ask me again and again to insert the input, until I give an input of one character only.

NOTE: it isn't in any sort of cycle loop, I don't know what the problem is. (I'm using cs50 library for the get char).

char nameless[] = { get_char("Insert the text here: ")};
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
markC
  • 1

1 Answers1

1
char nameless[] = {get_char("Insert the text here: ")};

If you look up get_char(), it clearly states

Prompts user for a line of text from standard input and returns the equivalent char; if text does not represent a single char, user is reprompted.

You need to enter only one character. Not a line of characters or 'text' as you call it.

Enter char: a

That's it.

Here's a working code example:

#include <cs50.h>

int main(void)
{
     // attempt to read character from stdin
     char c = get_char("Enter char: ");

     // ensure character was read successfully
     if (c == CHAR_MAX)
     {
         return 1;
     }

     char next = get_char("You just entered %c. Enter another char: ", c);

     if (next == CHAR_MAX)
     {
        return 1;
     }

     printf("The last char you entered was %c\n", next);
 }

You can look it up at CS50 Library for C

Capt_Steel
  • 25
  • 7
  • thanks, but my program requires a text (like from a book page) in input, so entering one character at time isn't what I'm looking for. Sorry but I'm new to programming. – markC Sep 01 '20 at 20:23
  • @markC that's totally not an issue. The best way to learn is to look things up. Also, please ask in your question **exactly** what you need. To take in a whole page of text and print it, take a look at this: https://stackoverflow.com/questions/3463426/in-c-how-should-i-read-a-text-file-and-print-all-strings – Capt_Steel Sep 01 '20 at 20:32
  • Thanks for understanding, I need to take in input a long text (like from a book Page) but not from a file but from the user's keyboard (like scanner), and then put that input in a char array (because I Need to count letters, words an sentences, but that's not a problem, I've already done that, I only Need to put that input in the array.) – markC Sep 01 '20 at 20:40
  • There's a ` get_string()` function, is there not, in the CS50 library. It returns a ”`string`” which is a `char *` in disguise. – Jonathan Leffler Sep 02 '20 at 04:36