Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Input Of an Unspecified Amount From a User

Posted on 1998-09-27
2
Medium Priority
?
246 Views
Last Modified: 2010-04-15
I am working on a project where the user enters in an unspecified amount of integers.  I havent came up with a good solution.

I can have the user enter the integers using scanf.  Scanf returns the amount of items entered I can use this to determine how much memory to allocate with malloc.  The problem is does scanf only count the valid data
(integers as opposed to characters?).  How would I handle if a character was in the mix of integers?


Am I headed in the right direction on this or does anyoen have a much better way of handling this.

Thanks
Darrell
0
Comment
Question by:larockd
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 1

Accepted Solution

by:
dotand earned 40 total points
ID: 1253087
Yoir best option would be to use a macro like isnum (I think that's the name on Borland C compiler, version 3.1).
If not than write your own, simply check the string until you hit a null and for each char check if it's the ascii range reserved for numbers.

That is, supposing s is the string in question and i an integer declared somewhere else that:


is_num(char *s){
  int i=0;
  while(s[i]){
    if(s[i]<='0' || s[i]<='9')
      return(0);
   }
   return(1);
   }
 }

Running atoi on the returned string won't help because there might be only a prtial conversion if it is a mixed string and no error returned. Better check using this method and then call atoi,
or are you lucky enough to call atol ?  ;-)>.

HTH,
Dotan
0
 
LVL 84

Expert Comment

by:ozo
ID: 1253088
What do you want to happen if a character was in the mix of integers?
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question