0

i am self taught programmer i face a problem here that if condition to enter only ( H OR T OR . ) doesn't get the string . go to the (printf ("Enter valid characters please \n");)

below is my code :

int i = 0 ;
int L ;

printf ("Please enter the lenght of report \n"); 
scanf ("%d" , &L);

if ( L>=1 && L<=500 )
{
        printf ("Please enter the Reoprt \n");
        string P = get_string ();
        if (P[i] !='H' && P[i] != 'T' && P[i] != '.')
        {
                printf ("Enter valid characters please  \n");
        }
        else 
        {
                printf ("GOOD3 \n");
        }
}
else 
{
        printf ("Please enter valid Length \n"); 
}
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • how is `i` treated? – Sourav Ghosh May 24 '17 at 10:07
  • Is this `C`? What does `string P` does? What `get_string()` function does? – Gaurav Pathak May 24 '17 at 10:07
  • 6
    @GauravPathak cs50.h, yes we all hate it. It is typedefed to char*. – Ajay Brahmakshatriya May 24 '17 at 10:07
  • 2
    And what is the problem with the code you show? For some example input, what is the expected and actual output? Have you tried stepping through the code line by line while monitoring variables in a debugger? – Some programmer dude May 24 '17 at 10:08
  • 2
    @Someprogrammerdude (_being sarcastic_) why? aren't you suppose to do my work for me? for free? :P – Sourav Ghosh May 24 '17 at 10:09
  • @AjayBrahmakshatriya I heard about `cs50.h` for the first time. Thanks! – Gaurav Pathak May 24 '17 at 10:09
  • 2
    Even if you are self-taught, you have heard about *loops*? It seems that's what you're looking for. And a [good beginners book](http://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list). – Some programmer dude May 24 '17 at 10:12
  • 1
    What is the issue? [Demo](https://ideone.com/ive1AB) works fine in my opinion? Are you compiling your code before running? (Assuming you want to check if the report begins with H or T or .) – Ajay Brahmakshatriya May 24 '17 at 10:12
  • The main problem is in `scanf `when i use cs50 functions the code runs well . I will try to focus more on the problem Thank you all – Mohamed Raslan May 24 '17 at 10:54
  • off topic: when the user runs this program, all the user sees is "Please enter the lenght of report" with a blinking cursor on the second line. You should always tell the user what they are expected to do. (BTW: `lenght` should be: `length`) Suggest: the prompt be: "Please enter the length of report (range: 1...500 inclusive)". Similar considerations exist for "Please enter the Reoprt" (BTW: `Reoprt` should be: `Report`) where the only valid characters are: `H`, `T`, and `.` When ever there is a problem with the input, output a error message and loop to try again. – user3629249 May 24 '17 at 16:12

1 Answers1

0

The reason you're having this problem is because scanf("%d", &L) will fetch one or more digits from the input and nothing else. As a result, it will leave behind the newline character that you entered after typing in your number.

When you call get_string() (I assume this is the same as GetString() in the CS50 library?), the first character it sees is this residual newline character, so all you're getting back is an empty string.

You can fix this easily enough. Just replace scanf ("%d" , &L); with L = GetInt();.

Or, alternatively, replace scanf ("%d" , &L); with scanf ("%d%c" , &L, &newline);, with newline declared as a char variable at the top of your main() function. This will consume the line break that follows the number, so that GetString() won't treat it as an empty string. For more details on how scanf() works, type man scanf at the command line.

r3mainer
  • 23,981
  • 3
  • 51
  • 88