GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 05-Jun-2003, 00:29
maddie maddie is offline
New Member
 
Join Date: Jun 2003
Posts: 8
maddie is an unknown quantity at this point

Including Maps and strings??


Hi,

Can someone explain to me what I'm doing wrong in my C++ program(c attachement).
I'm using maps and strings, so I've included the header files <map> and <string> and stil I get errors.
Can someone please help me out, cause I don't see what I'm doing wrong.

Thanks in advance,

Maddie
Attached Files
File Type: txt result3.txt (1.2 KB, 198 views)
  #2  
Old 05-Jun-2003, 05:31
jrobbio's Avatar
jrobbio jrobbio is offline
Regular Member
 
Join Date: Jan 2003
Location: Loughborough, England
Posts: 840
jrobbio will become famous soon enough
Just so everyone can see it in the new and snazzy highlighting: See sticky thread above this for help in doing it:

CPP / C++ / C Code:
#include <iostream.h>
#include <fstream.h>
#include <string>
#include <map>


#define AUDIO_BITS			short 13		//13 bits per sample short
#define SAMPLE_FREQUENCY	8000			//8khz sample rate
#define PI					2 * 3.141592653589793 //2PI

void main(){
	
	ifstream inputfile;
	const char *filename = {"d:/tdif.txt"};

	struct country{
		char name[15];
		map<string,string>tones;
	};
	map<string,string>global;
	map<string,country>countries;

	istream & read_country(istream & is, country & c, basic_string & name)
	{
		// at this point we have already read "Country = name"

		char ch;
		char tonetype[50];
		char value[4096];

		// use case insensitive comparison if you want to allow  
		// tone = or TONE = also.
		while (is >> name && name == "Tone") {
			if (! (is >> ch && ch == '=' && is >> 
				is.get(tonetype,sizeof(tonetype),',') && 
				is.get(value,sizeof(value)))) {
				// error in input data...throw some exception or something..
				// whatever you do, don't continue reading
			}
			// tonetype is the data before, and is used as key in the tones map.

			c.tones[tonetype] = value;
		}
		// at this point name is some other than "Tone".
		return is;
	}
	cin.get();
}
  #3  
Old 05-Jun-2003, 06:25
maddie maddie is offline
New Member
 
Join Date: Jun 2003
Posts: 8
maddie is an unknown quantity at this point
Question

Oh thanks... Now it looks much better...

But ehh... can you tell me if there's something wrong with my C++ program?? Cause everytime I compile this program I get the next error's:

--------------------Configuration: result3 - Win32 Debug--------------------
Compiling...
result3.cpp
D:\result3.cpp(18 ) : error C2143: syntax error : missing ';' before '<'
D:\result3.cpp(18 ) : error C2501: 'map' : missing storage-class or type specifiers
D:\result3.cpp(18 ) : error C2059: syntax error : '<'
D:\result3.cpp(18 ) : error C2238: unexpected token(s) preceding ';'
D:\result3.cpp(20 ) : error C2065: 'map' : undeclared identifier
D:\result3.cpp(20 ) : error C2065: 'string' : undeclared identifier
D:\result3.cpp(20 ) : error C2065: 'global' : undeclared identifier
D:\result3.cpp(21 ) : error C2065: 'countries' : undeclared identifier
D:\result3.cpp(21 ) : error C2275: 'country' : illegal use of this type as an expression
D:\result3.cpp(16 ) : see declaration of 'country'
D:\result3.cpp(23 ) : error C2061: syntax error : identifier 'basic_string'
D:\result3.cpp(24 ) : error C2601: 'read_country' : local function definitions are illegal
Error executing cl.exe.

result3.obj - 11 error(s), 0 warning(s)

--------------------------------------------------

So is it possible to explain me what I'm doing wrong. I use map and string in my program and I've included the header files <map> and <string> and still I get these error's.... I thaught it would work after having included the header files....

Can someone explain please??

All help is appreciated...thanks...
  #4  
Old 13-Jun-2003, 07:15
Garth Farley Garth Farley is offline
Invalid Email Address
 
Join Date: May 2002
Location: Ireland
Posts: 638
Garth Farley is a jewel in the roughGarth Farley is a jewel in the roughGarth Farley is a jewel in the rough
Sorry for the lateness of this reply.

I don't like the way you've put the struct there, try placing it outside the main(). Also I don't like structs at all, it would work better if you made the country into an object, like so:

CPP / C++ / C Code:
class Country{
    public:
        char[15] name;
        map<string,string> tones;
};

and use it pretty much in the same way (note capitalisation - handy for objects). But place it outside any function.

Also, after you declare the templated object, say map<string,string> have a space before the name of it.

Try those, and get back to me.
GF
  #5  
Old 13-Jun-2003, 08:11
maddie maddie is offline
New Member
 
Join Date: Jun 2003
Posts: 8
maddie is an unknown quantity at this point

I want to use key's instead of map's


Thanks for your help,

I've tried what you have adviced me to. But I get this error that map and string are undeclared identifiers. I've included the headerfiles map and string, but still get the error that map and string are undeclared identifiers.

So yet I don't know if I'm still doing something wrong or that this error is caused by something else which I still have to discover.


Anyway I think I'll try a different way instead of using maps I'll use key's, cause this is much easier.

Cause what I'm trying to do with this program is to read specific values from a textfile. So I thought that if I define the name's of the objects I need and run a sort of scan through the textfile by reading it line by line, I'd be able to get the values I need. Afterwards I could assign these values to the variabels used in my C++ program. To use these values to create a sinus and write the results to another file depending on the name, country and tonename.
That's why I wanted to use maps, but I think I've complicated it a bit. I think it's easier to use key's and run a scan through the textfile to detect the values.

This is the textfile I want to read from:
name = test
duration = 4/6
amplitude = 100 #Amplitude in percents

country = albania

tone = dial, 425, cont #continuous tone
tone = ringing, 425, 1.0-s4.0 #s stands for 'silence'
tone = busy, 425, 0.3-s0.3, 4, 90 #Overruled duration & amplitude
tone = congestion, 425, 0.15-s0.15, 4/6 #Overruled duration
------------------------------------------------------------------------
So that what is bold have to be the key's and that what is italic is the value I need to select and assign to variabels in my C++ program.
The results will than eventually be written to a file named <name_country_tonename.inp> example: test_albania_dial.inp

The prupose of my program is:
- Reading values from a textfile
- Assign the values to variabels in my c++ program
- Using the values to calculate the sinus
- Write the results to an inputfile (.inp).

This is how I want to calculate the sinus and write it to another file, but this file has to be able to get different names instead of only result. Cause everytime I get other values the results will be written to the same file and thus the file will be overwritten.

PHP Code:

while (t<=T)
    {
     result= (short) (a*sin(PI*f*t));        //creating a sinus
     resultfile.write ((char *) &result, sizeof (result.inp)); //writing it to a file named result.inp
     t += 125E-6;                            //instead of writing the sinus to result.inp,
    }                                        //I want to write the sinus to a file, where the 
    resultfile.close();                        //filename is dependant of name,country,tonename 
    cout<< endl<<"finished"<<endl;            //used in the inputfile. Thats why I want to read
}*/                                            //line by line from the imputfile so the values used 
    cin.get();                                //in this C++ program will stay updated. 


Let me know what you think and thanks for your help. Hope I'm clear otherwise I'll give you more information, but am not sure let me know ok...
  #6  
Old 14-Jun-2003, 16:25
Garth Farley Garth Farley is offline
Invalid Email Address
 
Join Date: May 2002
Location: Ireland
Posts: 638
Garth Farley is a jewel in the roughGarth Farley is a jewel in the roughGarth Farley is a jewel in the rough
Lemme see if I've got you right.

You want to parse a text file, with key=value, and insert the key/value pair into a key/map/linked-list so you can use the stuff like variables?

If I'm roughly there, give me a day to ponder over you're code. I've got my way of doing this, but it's very different to yours I'll try to suit your style!

I'll post up some code of mine once I've it written, K?

But what's worrying me the most is how some string & map are being called undeclared identifiers? Have you tested either of these, in a simple program?

GF
  #7  
Old 16-Jun-2003, 02:05
maddie maddie is offline
New Member
 
Join Date: Jun 2003
Posts: 8
maddie is an unknown quantity at this point
Yes indeed I've to parse the textfile. The key/ value have to be inserted into the key/ map/ linked-lists to use as variables.
Afterwards these variables will be used to calculate the sinus. The resultst of these calculations will be written to a different file.

Is it possible to complete this wthout using maps??

By the way I already have tested the header files string and map, string doesn't give any errors anymore but map still is. So what to do.

Thank you very much for your help.
  #8  
Old 24-Jun-2003, 03:24
maddie maddie is offline
New Member
 
Join Date: Jun 2003
Posts: 8
maddie is an unknown quantity at this point
Unhappy

Hi ,

Is it possible to define a few strings and than run a scan through the textfile, detecting the predefined strings?

Maddie
  #9  
Old 24-Jun-2003, 11:59
Garth Farley Garth Farley is offline
Invalid Email Address
 
Join Date: May 2002
Location: Ireland
Posts: 638
Garth Farley is a jewel in the roughGarth Farley is a jewel in the roughGarth Farley is a jewel in the rough
I'm really REALLY sorry for taking this long. I've the code written up at home, I'll post it ASAP (once I get my laptop working, it's in 50 pieces)

I've a seriously hectic new job, around 12 hours a day for the last 2 weeks, weekends inclusive! It's crazy, a company miles behind their deadline, and paying through the nose

I'll have the code up in a few hours.

GF
  #10  
Old 24-Jun-2003, 12:05
Garth Farley Garth Farley is offline
Invalid Email Address
 
Join Date: May 2002
Location: Ireland
Posts: 638
Garth Farley is a jewel in the roughGarth Farley is a jewel in the roughGarth Farley is a jewel in the rough
Just an FYI while you're waiting:

I've created 2 classes, one called a Tokeniser, which reads input and splits it up into chunks. So reading:
name = Bert
will have "name", "=", and "Bert".

Then using this, the second class (a templated class) is a special linked list class, which stores each key/value pair.

So the tokeniser reads a key, then an "=", then a value" it passes the key & value pair to the linked list. Then you can access the values of the keys by scanning through the list.

GF
 
 

Recent GIDBlogToyota - 2008 September Promotion by Nihal

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 21:24.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.