need help with input

This is a discussion on need help with input within the C Programming forums, part of the General Programming Boards category; hi, I have a question: I will be given an input : number1*number2 and the problem is I dunno how ...

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    25

    need help with input

    hi, I have a question: I will be given an input :
    number1*number2
    and the problem is I dunno how many digits they have....I will use them as "char array", so I'm thinking of using sscanf and fgets but they didn't help much...could you help me?

  2. #2
    Registered User 00Sven's Avatar
    Join Date
    Feb 2006
    Posts
    127
    I could help but I do not entirely understand the question. So you want the user to enter a number and then a * and then another number?

  3. #3
    Slave MadCow257's Avatar
    Join Date
    Jan 2005
    Posts
    735
    I could help but I do not entirely understand the question. So you want the user to enter a number and then a * and then another number?
    This is an extension of a post he made before

    You can allocate the array at runtime with malloc, or you could set the array to whatever the max size of the input is according to your instructor (looking back, 250 digits)

  4. #4
    Registered User
    Join Date
    Mar 2006
    Posts
    25
    let me explain it:examples of inputs:
    126735*988465432 (* = multiplication mark)
    or 3344*893267325
    so there will be two numbers and a multiplication mark * between them.I wanna get num1 and num2 and store them as char arrays.both two numbers can consist of 1 to 250 digits, so any number of digits between 1 and 250 they can have.as i don't know how long char arrays are how will I get them??

  5. #5
    Slave MadCow257's Avatar
    Join Date
    Jan 2005
    Posts
    735
    there might be a better way, but you could read it all into a 501 char buffer, then make a while loop that reads everything up to the * into your first array, and everything after the * into the second

  6. #6
    Registered User
    Join Date
    Mar 2006
    Posts
    25
    yes, that's what I'm trying to do but dunno how to do it as I started learning C just a 2 weeks ago...

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,663
    Well if each can be up to 250, then
    Code:
    char input[250+250+1+1+1];
    fgets( input, sizeof input, stdin );
    The +1's are
    - one for the *
    - one for the \n to mark the end of line, as returned by fgets()
    - one for the \0 to mark the end of the string

  8. #8
    Registered User
    Join Date
    Mar 2006
    Posts
    25
    ok, but I need two arrays seperately,...two numbers as two different char arrays!

  9. #9
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,074
    Then you would parse the single string into two strings afterwards. How difficult could it be considering you know what character seperates the two numbers?

    ...and don't yell at us, we're doing the best we can! *runs off and cries*
    Sent from my iPadŽ

  10. #10
    Registered User
    Join Date
    Mar 2006
    Posts
    25
    ok, the question is how will I parse it into two arrays?I've tried everything I know,it means I dunno enough and need your help....
    I've lloked up for lots of functions but none of them seemed helpful.

  11. #11
    Registered User
    Join Date
    Mar 2006
    Posts
    25
    for instance,I've tried sth like this:
    fgets(input, 503, stdin);
    sscanf(input, "%s %*c ", x);

    of course it's very stupid of me...

  12. #12
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,074
    This is the same problem you were asking about in this post, correct?

    In that post, you said it was homework, and while we'd like to help you with your homework, I can't be encourages to sit here and explain something that you've never been taught (as you said you've tried everything you know). So, what I think you should do, is reconsider how you're going to go about writing this program, because I seriously doubt your teacher is expected anything on it that he didn't teach you.

    Homework is reinforcement of what you learned in class. Everything you need to know is right there in your head, you just have to give it some thought. If you're still stuck, consider asking one of your classmates how they're doing it. We don't know how much you know, so we can't guide you through the whole assignment. Most of the more experienced programmers here can write this program a dozen different ways, and 11 of those ways would have your teacher asking how the hell you knew to do that.

    So it's up to you, if you want to keep asking how to write the assignment and risk getting an answer that leaves your teacher suspicious, then go ahead. But I'm sure that you know enough to get this problem done with nothing more than a little thought and ingenuity.

    Personally, I think catching a cheater in a programming course (not to imply you're straight out cheating) is like trying to find the sun during the day, because you know what the student knows and what they're capable of. When they have labs handed in with code they would never know it's clear they didn't write it themselves. Don't go searching though forums and websites looking for a function that can make this assignment easier because it's not what your teacher is expecting.
    Last edited by SlyMaelstrom; 03-04-2006 at 03:58 PM.
    Sent from my iPadŽ

  13. #13
    Slave MadCow257's Avatar
    Join Date
    Jan 2005
    Posts
    735
    As an extension of what SlyMaelstrom said, it sounds like you need to just delay finishing this assignment a bit and look back at what you've learned. For this part of the assignment all you need is the code Salem wrote and then a loop. So try reviewing loops, and the function Salem used

  14. #14
    Registered User
    Join Date
    Mar 2006
    Posts
    25
    in our department things go on like this:
    the instructor tells the theorical part, (not practical and never mentions how to use theorical part) he gives homeworks and expect us to learn use of some functions, use of some basic parts(like arrays ,characters,types etc.) I've been taking this course for 2 weeks and I dunno nothing about arrays,function-calls etc. I've tried to learn them by myself but I hardly can.we're supposed to learn while we're doing our homeworks ,so my head is empty about c-programming,did I make myself clear?

  15. #15
    Registered User
    Join Date
    Mar 2006
    Posts
    25
    my classmates are desperate too, except 2 or 3 guys who knows c-programming well or taking this course 3rd or 4th times.

Page 1 of 3 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem grabbing k/b input
    By falcon9 in forum C Programming
    Replies: 2
    Last Post: 10-28-2007, 11:47 AM
  2. continues input
    By Matty_Alan in forum C Programming
    Replies: 2
    Last Post: 06-22-2007, 10:04 PM
  3. Input statement problem
    By une in forum C Programming
    Replies: 3
    Last Post: 05-29-2007, 11:16 PM
  4. For loop problems, input please.
    By xIcyx in forum C Programming
    Replies: 2
    Last Post: 04-22-2007, 03:54 AM
  5. Simple Console Input for Beginners
    By jlou in forum C++ Programming
    Replies: 0
    Last Post: 06-21-2005, 01:50 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21