Very Stupid C++ Questions

Aug 14, 2001
11,061
0
0
Well, it's been at least 4-5 years since I've even worked with some C++ programming, but I need to refresh in it right now. I had a couple of really ridiculously easy questions that I'm hoping that someone can help me out on that I've blanked on the syntax upon.

I have two made up input data files:

input1.dat with the following contents:
monkey 5.1
snail 7.2e-9
carrot 6.6

input2.dat with the following contents:
monkey 9.8
snail 4.2e-5
carrot -6.6

How would I step through each of the input files and obtain all of the names in a string array containing each name (monkey, snail, or carrot) in its own element so that later I can call up each name separately ie, I'm trying to get an array containing 'monkey' as the first index, 'snail' as the second index, and 'carrot' as the third index. After reading in the name, I would also need to read in the numerical value following the name into a separate variable array.

So, in the end, I'm trying to get array_name['monkey','snail','carrot'] and
input1values[5.1,7.2e-9,6.6] and input2values[9.8,4.2e-5,-6.6].

Thanks.
 

DaShen

Lifer
Dec 1, 2000
10,710
1
0
Do you honestly think someone is going to write the program for you. When it is so obvious it is a school assignment? Just wondering.
 

NuroMancer

Golden Member
Nov 8, 2004
1,684
1
76
Originally posted by: RabidMongoose
Well, it's been at least 4-5 years since I've even worked with some C++ programming, but I need to refresh in it right now. I had a couple of really ridiculously easy questions that I'm hoping that someone can help me out on that I've blanked on the syntax upon.

I have two made up input data files:

input1.dat with the following contents:
monkey 5.1
snail 7.2e-9
carrot 6.6

input2.dat with the following contents:
monkey 9.8
snail 4.2e-5
carrot -6.6

How would I step through each of the input files and obtain all of the names in a string array containing each name (monkey, snail, or carrot) in its own element so that later I can call up each name separately ie, I'm trying to get an array containing 'monkey' as the first index, 'snail' as the second index, and 'carrot' as the third index. After reading in the name, I would also need to read in the numerical value following the name into a separate variable array.

So, in the end, I'm trying to get array_name['monkey','snail','carrot'] and
input1values[5.1,7.2e-9,6.6] and input2values[9.8,4.2e-5,-6.6].

Thanks.

All the hints u need,

String Tokenizer

Edit: /thread
 

BigJ

Lifer
Nov 18, 2001
21,330
1
81
Originally posted by: NuroMancer
Originally posted by: RabidMongoose
Well, it's been at least 4-5 years since I've even worked with some C++ programming, but I need to refresh in it right now. I had a couple of really ridiculously easy questions that I'm hoping that someone can help me out on that I've blanked on the syntax upon.

I have two made up input data files:

input1.dat with the following contents:
monkey 5.1
snail 7.2e-9
carrot 6.6

input2.dat with the following contents:
monkey 9.8
snail 4.2e-5
carrot -6.6

How would I step through each of the input files and obtain all of the names in a string array containing each name (monkey, snail, or carrot) in its own element so that later I can call up each name separately ie, I'm trying to get an array containing 'monkey' as the first index, 'snail' as the second index, and 'carrot' as the third index. After reading in the name, I would also need to read in the numerical value following the name into a separate variable array.

So, in the end, I'm trying to get array_name['monkey','snail','carrot'] and
input1values[5.1,7.2e-9,6.6] and input2values[9.8,4.2e-5,-6.6].

Thanks.

All the hints u need,

String Tokenizer

Edit: /thread

Also, please PLEASE post in the Software/Programming Forum. That is the only way that the Mods may ever consider actually breaking off Programming from that dreaded forum into it's own section.
 

cliftonite

Diamond Member
Jul 15, 2001
6,899
63
91
Originally posted by: RabidMongoose
Well, it's been at least 4-5 years since I've even worked with some C++ programming, but I need to refresh in it right now. I had a couple of really ridiculously easy questions that I'm hoping that someone can help me out on that I've blanked on the syntax upon.

I have two made up input data files:

input1.dat with the following contents:
monkey 5.1
snail 7.2e-9
carrot 6.6

input2.dat with the following contents:
monkey 9.8
snail 4.2e-5
carrot -6.6

How would I step through each of the input files and obtain all of the names in a string array containing each name (monkey, snail, or carrot) in its own element so that later I can call up each name separately ie, I'm trying to get an array containing 'monkey' as the first index, 'snail' as the second index, and 'carrot' as the third index. After reading in the name, I would also need to read in the numerical value following the name into a separate variable array.

So, in the end, I'm trying to get array_name['monkey','snail','carrot'] and
input1values[5.1,7.2e-9,6.6] and input2values[9.8,4.2e-5,-6.6].

Thanks.



You need to use streams if & ofstream i believe
 

blahblah99

Platinum Member
Oct 10, 2000
2,689
0
0
Originally posted by: RabidMongoose
Well, it's been at least 4-5 years since I've even worked with some C++ programming, but I need to refresh in it right now. I had a couple of really ridiculously easy questions that I'm hoping that someone can help me out on that I've blanked on the syntax upon.

I have two made up input data files:

input1.dat with the following contents:
monkey 5.1
snail 7.2e-9
carrot 6.6

input2.dat with the following contents:
monkey 9.8
snail 4.2e-5
carrot -6.6

How would I step through each of the input files and obtain all of the names in a string array containing each name (monkey, snail, or carrot) in its own element so that later I can call up each name separately ie, I'm trying to get an array containing 'monkey' as the first index, 'snail' as the second index, and 'carrot' as the third index. After reading in the name, I would also need to read in the numerical value following the name into a separate variable array.

So, in the end, I'm trying to get array_name['monkey','snail','carrot'] and
input1values[5.1,7.2e-9,6.6] and input2values[9.8,4.2e-5,-6.6].

Thanks.

if(!(format(mylife)) {
jumpoffbridge(); }
 
Aug 14, 2001
11,061
0
0
Originally posted by: DaShen
Do you honestly think someone is going to write the program for you. When it is so obvious it is a school assignment? Just wondering.

I'm actually an electrical engineer working and I have a text file of data that I need to use, but I'm I'm not good at scripting these right now. I don't believe anyone would have a school assignment at this time in August, but I'm also not looking for anyone to really write any type of program.

All I'm concerned about is reading a text file composed of a string and then a number into two separate arrays.
 

EagleKeeper

Discussion Club Moderator<br>Elite Member
Staff member
Oct 30, 2000
42,589
5
0
Figure out how to read each record in the file.

Now parse the record into data fields.

Create an structure of data for the information

Put the structure into an array/vector for sorting later on.
 

Zombie

Platinum Member
Dec 8, 1999
2,359
1
71
Originally posted by: EagleKeeper

Now parse the record into data fields.

easier said than done :)

Use the link I gave above. It will allow you to atleat open and read file. Parsing could be a headache if you need to run this on unix.
 
Aug 14, 2001
11,061
0
0
Originally posted by: Zombie
Originally posted by: EagleKeeper

Now parse the record into data fields.

easier said than done :)

Use the link I gave above. It will allow you to atleat open and read file. Parsing could be a headache if you need to run this on unix.

Thanks for the link, I'll look into it. It doesn't have to necessarily work on a Unix platform this very moment, but parsing the information is my main or only problem right now. I don't have a problem in reading or opening the file, but in splitting up the data lines.
 

yourdeardaniel

Golden Member
Mar 20, 2004
1,905
0
0
is there a space between each word and value? in java you can use string tokenizer

Scanner read = new Scanner(new File("data.txt"));
input = read.nextLine();
tokenizer = new StringTokenizer(input, " ");

word = tokenizer.nextToken();
value = tokenizer.nextToken();

can also do string .split() or scanner .useDelimiter()

i dunno how to do it in C++, taking that now -_-
 

EagleKeeper

Discussion Club Moderator<br>Elite Member
Staff member
Oct 30, 2000
42,589
5
0
a string function has a search that would allow you to get an index to a character.

If the space (0x20) is your field deliniator, then find the space and the first part of the string to that point is your string data, then rest of the string would be your numeric data.

Up to you how to handle each type of data.