473,781 Members | 2,280 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

n00b input help

Hi,
I'm writing a program that performs arithmetic operations on integers.
I want to be able to type in a bunch of integers seperated by any
amount of white space then terminate input with a non-integer
character. I plan to put my input into an array, and while I have a max
size I don't have a min and don't know exactly how many arguments to
expect.
I would really appreciate any ideas.

Cheers.

Apr 1 '06 #1
5 1490
moostafa wrote:

Hi,
I'm writing a program that performs arithmetic operations on integers.
I want to be able to type in a bunch of integers seperated by any
amount of white space then terminate input with a non-integer
character.
You can terminate with the Enter key, instead, just as simply.
I plan to put my input into an array, and while I have a max
size I don't have a min and don't know exactly how many arguments to
expect.
I would really appreciate any ideas.


Are these integers coming in as command line arguments
or as querried input?

--
pete
Apr 1 '06 #2
moostafa wrote:

I'm writing a program that performs arithmetic operations on
integers. I want to be able to type in a bunch of integers
seperated by any amount of white space then terminate input with
a non-integer character. I plan to put my input into an array,
and while I have a max size I don't have a min and don't know
exactly how many arguments to expect.
I would really appreciate any ideas.


The usual solution for input of unknown length is a linked list.
Once you have determined the total input you can then use it as is,
sort it, convert it into an array, or what-not. Linked lists are
best sorted with mergesort.

--
"If you want to post a followup via groups.google.c om, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell. org/google/>
Also see <http://www.safalra.com/special/googlegroupsrep ly/>
Apr 1 '06 #3
> You can terminate with the Enter key, instead, just as simply.

It's for a school assignment, otherwise I would just leave it as the
enter key.
Are these integers coming in as command line arguments
or as querried input?
I'm not sure what you mean by command line arguments. I want it to look
something like this:

Computing, please input list of integers 1 2
3 4
a The sum of your integers is etc. etc.

I tried using scanf but it only ignores the new line if you specify how
many arguments you're expecting - which I can't do.
The usual solution for input of unknown length is a linked list.
Once you have determined the total input you can then use it as is,
sort it, convert it into an array, or what-not.


Don't you still need to read the input using gets or something to put
it into a linked list? I'm not really sure how they work, but I thought
that if I was giving my input all at once it would just go into one
list.

Apr 2 '06 #4
"moostafa" <mo**********@h otmail.com> writes:
[...]
Don't you still need to read the input using gets or something to put
it into a linked list? I'm not really sure how they work, but I thought
that if I was giving my input all at once it would just go into one
list.


Never use gets(). If you want to read a line at a time (which is
usually the right thing to do), use fgets(). gets() cannot be used
safely (except in rare and obscure circumstances you're unlikely ever
to run into), and there's a great deal of support for removing it from
the language.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Apr 2 '06 #5
On Sat, 01 Apr 2006 16:42:57 -0800, moostafa wrote:
You can terminate with the Enter key, instead, just as simply.


It's for a school assignment, otherwise I would just leave it as the enter
key.
Are these integers coming in as command line arguments or as querried
input?


I'm not sure what you mean by command line arguments. I want it to look
something like this:

Computing, please input list of integers
> 1 2
> 3 4
> a

The sum of your integers is etc. etc.

I tried using scanf but it only ignores the new line if you specify how
many arguments you're expecting - which I can't do.


I don't know what you mean by this. Writing:

scanf("%d", &number)

will read an integer into the int variable "number" (after skipping over
any white space characters). The return value will be either 1 (meaning 1
number read and converted); 0 (meaning something was found that could not
be part of a number) or EOF (meaning that something else went wrong). It
is not a coincidence that this is almost exactly what you want -- a number
read in or an indication of what went wrong if no number was found!
The usual solution for input of unknown length is a linked list. Once
you have determined the total input you can then use it as is, sort it,
convert it into an array, or what-not.


Don't you still need to read the input using gets or something to put it
into a linked list? I'm not really sure how they work, but I thought that
if I was giving my input all at once it would just go into one list.


This reads like an early assignment so a linked list may be overkill. You
need a list (or some other storage) if the calculations you want to do
require all the numbers to available at once (for example finding the
middle number[1]) but not if you want to just add them up.

And may I second the advice from KT -- don't use gets. Ever. You need to
read the number and calculate with it or store it somewhere (like a linked
list) if you need to keep them all for later.

[1] in the most general case.

--
Ben.
Apr 2 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
2134
by: Christo | last post by:
borland c++ 5.01 character constant must be one or two characters long get this when compiling my first c++ program can anyone out there help? it is highlighting this line as the problem cout << "Please Enter First number: ";
1
1544
by: newgenre | last post by:
I am using a pre-built package of code for my site, which is called EasyDisc. All it does is it creates an interactive forum on your site, like any forum you see anywhere. I am having a problem getting started as I am new to .NET and sql. The app, once unzipped, tells me to "Make note of your SQL server name, username, password, database name. You need to supply these info. later." My problem is I don't know where to find out that info....
1
225
by: CosmicLogick | last post by:
This following piece of code wont work, i have no idea what i did wrong. Data.mdb is the database file Admin is the table username and password are fields oldpasstxt.Text and newpasstxt.Text are from input boxes. The error vs.net gives me is olecmd.ExecuteNonQuery()
4
10146
by: onefry | last post by:
Hey I have this prog that i'm working on, starting my first c++ class and kind of a n00b to programming here it is #include <iostream> #include <cstdlib> using namespace std;
14
1449
by: TuperYabba | last post by:
Hello. I'm a noob. As such, I will now ask the obligatory stupid questions. Can anyone tell me how to do this??? I need to write a program that acts as a database for a CD collection, It has to use a class to store the CD info, has to have a file saving implemented, and must use dynamic data. Thank you for any help.
6
1506
by: Charles | last post by:
I am learning from the Accelerated C++ book. The following example doesn't work and I don't know why: #include <iostream> #include <string> int main () { const std::string exclam = "!"; const std::string message = "Hello" + ", world" + exclam; return 0; }
8
1423
by: HardHackz | last post by:
Hey, I'm trying to learn C++, the problem is, when I do cout << "Hello World!"; it always opens dos and closes it to quickly to see...i know im a total n00b, but any help?
5
1471
by: chaoticcranium | last post by:
Right now, I am reading a file line by line using getline() using this code: // Read file line by line, store the first 3 values on each line in a vector and store the vector in a vector of vectors while(getline(peakReader, line)) { istringstream lineStr(line); vector<intpeak(3); if (!(lineStr >peak >peak >peak)) { cerr << "Error reading peak file; will continue
0
1135
by: hockeyjk | last post by:
All, I'm writing a program that creates a histogram of data. IDLE is freezing up after the window opens (doesn't prompt user or graph anything). The window that opens is named "tk" rather than the name I assign to it. Can anyone shed light on why. EDIT: Nevermind. The error was resulting because I was attempting to run the file from my flash drive. Who knew. n00b. Can an admin delete this thread?
0
9474
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10306
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9931
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8961
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7485
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5373
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5504
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4037
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3632
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.