Tagged: ,

  • This topic is empty.
Viewing 1 post (of 1 total)
  • Author
    Posts
  • #311
    admin
    Keymaster

      Since CS50 IDE not loading on my desktop, I thought to use some other free online IDE. Came to know about GeeksforGeeks and decided to use it. I know functions like get_int not possible as without cs50.h library.

      Here is the program I wrote ( https://ide.geeksforgeeks.org/lPoZXRZntH ).

      
      
      #include<stdio.h>
      int main() 
      {
          int t = 5;
          int num[t];
              for(int i = 0; i < t; i++)
              {
              printf("enter");
              scanf("%d",&num);
              printf("%d",num);
              }   
      }

       

      I expected a screen to appear with “enter” after which I will put 5 numbers one by one. But instead, getting a strange output:

      enter272491544enter32765enter15775231enter0enter1h

      Here is how it worked for me:

      r/cs50 - Working outside CS50 IDE: Hurdle with scanf command

      r/cs50 - Working outside CS50 IDE: Hurdle with scanf command


      Reply


      I don’t quite understand what is your question. The code is working as it should. For each interaction of the for loop, it prints “enter”, and allow you to enter a number, and prints this number after and repeat.

      If you don’t want to repeat the “enter”, put the first printf outside the loop. If you think it’s strange all in the same line, you have to use “\n” on printf to print a newline.


      As said, the program seems to work as intended. However, I was able to reproduce a similar output to yours by entering garbage data as input. You have no control of the user input here. I would suggest you add some kind of verification that the entered number is actually a number 🙂


      OP


      Still not clear as I am not finding any prompt space to enter input.

      The below command should prompt a space to enter input!

      scanf(“%d”,&num);

      So how without entering any input, getting output.


      Reply


      I don’t really know what you mean by that. scanf doesn’t prompt, or display anything, it just reads standard input.

      What I think you’re interpreting as a “prompt” is just the side effect of the fact that you would normally be running your program in a terminal, which displays your input in-line with your output. That only happens because your standard input on a terminal is you typing directly into it. If your standard input is coming from somewhere else (such as a file, which is likely how that site does it) then your input won’t be displayed along with your output because your input and output are not rolled into a single source like they are in a terminal.


      OP


      This is a difference between running your program in a terminal and not running your program in a terminal.[learn_press_profile]

    Viewing 1 post (of 1 total)
    • You must be logged in to reply to this topic.
    Scroll to Top