Thread: Storing an integer in an array using getchar

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    81

    Storing an integer in an array using getchar

    I'm trying to get getchar to put three numbers which the user will enter as a 3 digit integer into an array. This is part of a bigger program which I believe if I can get this to work I will be able to complete the rest. But compiler tells me there are too many arguments for getchar.

    Code:
    #include<stdio.h> void main()
    
    
    {
    
    
    int num[3]; /*Sets array to 10*/
    int count;
    
    
    printf("Enter 10 numbers:");
    
    
    	for(count=0; count<3; count++){
                getchar(num);
                
      }
    		
    		
    return;
    
    
    }

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Look at the documentation for getchar: gets(3) - Linux manual page. Where do you see it taking a parameter? What about it's return value? Do you do anything with that? Don't just guess at how to use a function, look it up. Then, read a tutorial or two on arrays (Arrays in C - Cprogramming.com).

    And please, it's int main(void) and return an integer at the end. I'm pretty sure we've been over this before.

  3. #3
    Registered User
    Join Date
    Oct 2011
    Posts
    81
    What you're telling me to do though is exactly what my lecturer tells me not to. This is so confusing.

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    If your lecturer is telling you to
    1. Not do any thinking or research on your problem
    2. Not learn anything on your own
    3. Not comply with the C standard (i.e. using void main instead of int main; but then, this post of yours uses int main, so make up your mind)

    Then you really need to talk to the department head. Otherwise, I don't know what I said to do that your lecturer said not to. Please be explicit.

    I've seen your other comments about how much this guy sucks, but honestly, if you wont drop it, then you need to take initiative or stop whining about it. Really, with all the college experience you've boasted, you should be mature and experienced enough to deal with something like this. Go to the department head with some concrete evidence of just how worthless this guy is and have them do something about it. Hell, write your representative or minister of education or whatever.

    But here, you're in the "C Programming" lecture hall. We're the teachers/professors. You play by our rules here. Go look it up and learn a little on your own. I know you're smart enough to. If nothing else, you'll end up smarter than your prof.
    Last edited by anduril462; 11-03-2011 at 09:48 AM.

  5. #5
    Registered User
    Join Date
    Oct 2011
    Posts
    81
    Quote Originally Posted by anduril462 View Post
    If your lecturer is telling you to
    1. Not do any thinking or research on your problem
    2. Not learn anything on your own
    3. Not comply with the C standard (i.e. using void main instead of int main; but then, this post of yours uses int main, so make up your mind)

    Then you really need to talk to the department head. Otherwise, I don't know what I said to do that your lecturer said not to. Please be explicit.

    I've seen your other comments about how much this guy sucks, but honestly, if you wont drop it, then you need to take initiative or stop whining about it. Really, with all the college experience you've boasted, you should be mature and experienced enough to deal with something like this. Go to the department head with some concrete evidence of just how worthless this guy is and have them do something about it. Hell, write your representative or minister of education or whatever.

    But here, you're in the "C Programming" lecture hall. We're the teachers/professors. You play by our rules here. Go look it up and learn a little on your own. I know you're smart enough to. If nothing else, you'll end up smarter than your prof.
    Who says I haven't? I have been banging on the door of various people in the college for weeks and nothing is done. Maybe you're in America where things work much better than they do here. In America if someone f***s up they get fired, here they get promoted, just look at our government for proof...

    And I haven't intended it to come across as boasting, I was simply trying to evidence (a)that I know how colleges work and am not a teenager just whining and (b)that I'm not stupid either.

    Although, being stupid has been suggested to me by the college as the problem why I can't understand C.

  6. #6
    Registered User
    Join Date
    Oct 2011
    Posts
    81
    I changed it around as per your instructions. It wouldn't compile, then I tried it as void main() and return; and it compiled. Look, I'm not saying your wrong, obviously you know more than me, but why is that. It still spits out wrong stuff but it does compile.

    I'm not trying to be whiny but I have never learned anything that I couldn't get my head around. You've seen the questions, and my homework. I'm at this for 20-30 hours per week for 8 weeks and I'm still completely s***e at it. I'm not getting anywhere, please understand how frustrating it is to work that hard at something and have made no progress.

  7. #7
    Registered User
    Join Date
    Mar 2009
    Posts
    344
    I'm not a die-hard about int main(void) as others around here, but if you're using a compiler which can't handle that line of code, there are real problems with the tool. Or more likely, you've got something else wrong with the code. Post the non-working version and maybe we can help clear up the problem.

  8. #8
    Registered User
    Join Date
    Oct 2011
    Posts
    81
    Quote Originally Posted by KCfromNC View Post
    I'm not a die-hard about int main(void) as others around here, but if you're using a compiler which can't handle that line of code, there are real problems with the tool. Or more likely, you've got something else wrong with the code. Post the non-working version and maybe we can help clear up the problem.
    Code:
    #include<stdio.h> int void main()
    
    
    {
    
    
    int num[3]; /*Sets array to 10*/
    int count;
    
    
    printf("Enter 10 numbers:");
    
    
    	for(count=0; count<3; count++){
                getchar();
                
      }
      
      printf("%d");
    		
    		
    return 0;
    
    
    }
    Its supposed to store the numbers in the array then print the number. I have to print the number then also print the numbers as separate, for example, 123 then 1, 2 and 3.

    I reckon if I could get as far as printing 123 then 1, 2 and 3 would be the least of my problems.

    As I said, I don't want to seem like I'm whiny. I'm not, I love learning, but if something is explained then I can learn if its not then I can't (that's not a dig at you guys, you're most helpful, its by way of explanation of why I moan about my lecturer).

    Seriously, the only reason I'm sticking this out is because I want to go to live in Italy and I know I've a better chance of a better job there in this line of work than in what I am doing at the moment.

  9. #9
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Interista View Post
    Code:
    #include<stdio.h> int void main()
    
    
    {
    
    
    int num[3]; /*Sets array to 10*/
    int count;
    
    
    printf("Enter 10 numbers:");
    
    
    	for(count=0; count<3; count++){
                getchar();
                
      }
      
      printf("%d");
    		
    		
    return 0;
    
    
    }
    Its supposed to store the numbers in the array then print the number. I have to print the number then also print the numbers as separate, for example, 123 then 1, 2 and 3.
    Code:
    #include <stdio.h> 
    #include <ctype.h>
    
    int main(void)
    {
       int num[10]; /*Sets array to 10*/
       int count = 0;
       int i;
    
       do
         {
            printf("Enter digit #%d  :  ", count);
            num[count] = getchar();
            if (isdigit(num[count] )
              count++
            while(getchar() != '\n'); 
          }
       while(count < 10);           
      }
      for (i = 0; i < 10; i++ )  
         printf("%d");
    	
       return 0;
    }
    Now the thing is, I've seen you post code at this level before... Blaming your teacher works only so far. If you show us that you CAN write code like this --and you have-- then you lose all excuses for that mess you posted... You don't get to blame the teacher when you are backsliding in your skills.

  10. #10
    Registered User
    Join Date
    Mar 2009
    Posts
    344
    Quote Originally Posted by Interista View Post
    Code:
    #include<stdio.h> int void main()
    Despite using the same words, "int void main()" is not the same as "int main(void)". Plus you should have it start on its own line. Plus you never assign anything to num, so no wonder it's not getting changed. Plus even if you did, you're never printing it out so how would we know even if it was set successfully?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 08-11-2011, 02:37 PM
  2. Replies: 1
    Last Post: 11-05-2009, 12:30 AM
  3. Array of getchar / count
    By Marth_01 in forum C Programming
    Replies: 20
    Last Post: 11-19-2008, 07:36 PM
  4. storing integer values in files using WriteFile
    By Fender Bender in forum Windows Programming
    Replies: 1
    Last Post: 01-15-2006, 12:15 AM