Thread: Enter an integer????

  1. #1
    Princess of code universe programmer1's Avatar
    Join Date
    Nov 2011
    Location
    Wonderland
    Posts
    38

    Enter an integer????

    Helloooooo, I have one question! Is this any way to ask " Enter an integer" for n time?Instead of "Enter integers" and entering integer for n time? I tried printf but it didnot work!??

    Code:
    #include<stdio.h> 
    void main() 
    { 
    int n,m 
    
    
    printf("Please enter a positive integer:"); 
    scanf("%d",&n); 
    printf("Enter integers:"); 
    scanf("%d",&m);

  2. #2
    Registered User
    Join Date
    Nov 2011
    Location
    Saratoga, California, USA
    Posts
    334
    Until you understand loop constructs and/or arrays - variable length arrays in particular - you will not be able to control the number of inputs read based on 'n'.

    Your scanf("%d", &m); executes once and only once and stores one and only one integer in the variable m.

  3. #3
    Princess of code universe programmer1's Avatar
    Join Date
    Nov 2011
    Location
    Wonderland
    Posts
    38
    So, you mean it is not possible without arrays?

  4. #4
    Registered User
    Join Date
    Nov 2011
    Location
    Saratoga, California, USA
    Posts
    334
    It depends on whether you want to store the data or just output it in some way as soon as it is entered.

    With a loop, you could read the input, store it in a single variable, optionally modify it, and output it... repeat, overwriting the variable each time.
    If you want all the data stored, you would need an array or at least enough int variables to handle the input. An array being the better choice.

  5. #5
    Princess of code universe programmer1's Avatar
    Join Date
    Nov 2011
    Location
    Wonderland
    Posts
    38
    ı am sorry I am a beginner I can not understand the loop part . You mean, if I wanna use loops, I have to identify a,b,and c instead of m if n=3 and I have to write printf and scanf for 3 times. Is it correct?

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by programmer1 View Post
    ı am sorry I am a beginner I can not understand the loop part . You mean, if I wanna use loops, I have to identify a,b,and c instead of m if n=3 and I have to write printf and scanf for 3 times. Is it correct?
    Any time you find yourself asking "How often do I want to do this?"... you're probably in a good position to use loops (for() while() or do while())

  7. #7
    Registered User
    Join Date
    Nov 2011
    Location
    Saratoga, California, USA
    Posts
    334
    I know you're a beginner. I was basically telling you that until you get to at least loops in your learning you can't do what you are asking. Have patience.

    And no, I meant quite the opposite. If you don't want to store the data, you can use a loop and have just one variable, m.

  8. #8
    Princess of code universe programmer1's Avatar
    Join Date
    Nov 2011
    Location
    Wonderland
    Posts
    38
    @CT and @ Tclausex, thanks for your answer I am trying to understand the logic of for but I didnt yet. For example in the exam there are many "for" that made up a triangle. I did it. But let me ask, we are always starting the loop inside,right? I mean if I am working on nested loops, first operation will be inside,right?

  9. #9
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    In nested loops the intial values are set as it tunnels in, then the inner most loop runs a full cycle for each count of the loop above it...

  10. #10
    Princess of code universe programmer1's Avatar
    Join Date
    Nov 2011
    Location
    Wonderland
    Posts
    38
    Thanks for the help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 11
    Last Post: 05-10-2009, 08:51 AM
  2. Replies: 9
    Last Post: 02-22-2009, 11:50 PM
  3. Replies: 5
    Last Post: 06-12-2007, 02:18 PM
  4. determining if an integer is a legal integer
    By LiquidBlaze33 in forum C++ Programming
    Replies: 2
    Last Post: 10-09-2005, 07:06 PM
  5. finding an integer in an integer
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 05-09-2002, 07:25 PM