0

I'm a beginner in programming, and I'm currently working on C.

I'm trying to prompt the user for an input. The input I'm trying get is a block of text from them. But whenever a sentence is given, the program keeps prompting and the loop won't stop. What can I do to stop the loop once the text is inputted?

This is what I got so far:

#include <cs50.h>
#include <stdio.h>
#include <math.h>
#include <ctype.h>
#include <string.h>

int main(void)
{
    //Prompt user for input
    long p;
    int s;
    do
    {
        p = get_long ("Insert Text Here: ");
        scanf("%li \n", &p);
        p = (char) p;
    }
    while ((p < 'a' && p > 'Z') || (p < 'A' && p > 'Z'));
    
    //Analyze The Grade Level
    s = 0;
    while (s < p)
    {
        s++;
        printf ("s: %i \n", s);
    }
    printf ("\n");
}
        
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Kate_B
  • 19
  • 1
  • 8
    Make a habit of speaking these out aloud/in your head. `(p < 'A' && p > 'Z')`. "While p is less than A and at the same time larger than Z". Wait, that doesn't make any sense... – Lundin Oct 16 '20 at 09:57
  • 2
    What is the purpose of `scanf("%li \n", &p)` here? `get_long` is already doing the job. And please [edit] the question and show a simple example of input and expected output. – Jabberwocky Oct 16 '20 at 10:00
  • 2
    Also, both `Z`'s are capitals in the first `while` loop. – Elliott Oct 16 '20 at 10:01
  • Note that `scanf("%li \n", &p);` looks for a non-whitespace character after the number that you enter. This is bad; do not include white space at the end of a `scanf()` format string. – Jonathan Leffler Oct 30 '20 at 17:28

0 Answers0