Computing.Net > Forums > Programming > syntax error

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

syntax error

Reply to Message Icon

Name: csue103
Date: January 14, 2005 at 12:28:05 Pacific
OS: XP
CPU/Ram: 512
Comment:

i don't figure what's the error here can anyone help

#include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>

using namespace std;

class Histogram{
public:
string word;
string msg[];
int array[];
int length;
int freq[];
ifstream infile;

void open();
void frequency(int array[],int count);
int alpha();
};

void Histogram::open()
{
infile.open("msg.txt");

if(infile.fail())
{
cout<<"File cannot be open"<<endl;
exit(0);
}
infile>>word;
length=strlen(word.c_str());

for(int i=0;i<length;i++)
{
infile >> msg[i];

if(infile.eof())
break;
}

infile.close();
frequency(msg[],length);
}


void Histogram::frequency(int array[],int count)
{
atoi(array[].c_str());
for(int i=0;i<26;i++)
freq[i]=0;
for(int j=0;j<count;j++)
++freq[array[j]];
for(int rating =1;rating<=26;rating++)
{
cout <<freq[rating];

for(int k=1;k<=freq[rating];k++)
{
cout <<'*';
cout<<endl;
}
}
system("pause");

}



Sponsored Link
Ads by Google

Response Number 1
Name: bamakhrama
Date: January 15, 2005 at 01:21:40 Pacific
Reply:

Hi,
You have a group of errors:
1) You can NOT define an array in C++ without giving it's size.
e.g. string msg[2];
int array[2];
int length;
int freq[2];

2) in the last line of the constructor, you are calling frequency(msg[],length);
How can you do that while you defined frequency() to be void frequency(int array[],int count);
3) In the 1st line of frequency, you are calling atoi() on an array of integers! atoi takes const char* as input.

--
mohameda@ieee.org


0

Response Number 2
Name: csue103
Date: January 15, 2005 at 05:02:06 Pacific
Reply:

thanks for reminding me..
but i still don't understand how am i going to change the string array into a const char array..
thanks for ur help!!


0

Response Number 3
Name: BlueRaja
Date: January 15, 2005 at 11:59:32 Pacific
Reply:

Wow, ignore that last post, susu
"You can NOT define an array in C++ without giving it's[sic] size."
-Yes you can, it's called a pointer.
This declaration:
void frequency(int array[],int count);
is just fine.
However, when not used as an argument to a function, you should just declare it as a pointer:
int* array;

"in the last line of the constructor, you are calling frequency(msg[],length);
How can you do that while you defined frequency() to be void frequency(int array[],int count);
"
-Uhh...there is no constructor defined in this code.
He is right, however, that you can't send msg[] as an argument. For one thing, that's not a valid argument. If you want to send the address of msg to the function (which would be useless, because both functions are members of the same class), you'd have to use simply msg (assuming msg is an array of strings), or &msg (if msg is just a string variable, which I believe is what you want it to be)

Now, onto your code. Susu, I'm not entirely sure what you're trying to do here. What is the purpose of the array[] array in Frequency()? What is the purpose of the seperate *array array in Histogram? Why are you trying to make an array of strings?
I'm going to need more information about what your crazy code is trying to do before I can help you any further.

Hope that helps.

BlueRaja.admin@gmail.com



0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More


Large Random Numbers in C... Update Batch file from Ba...



Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: syntax error

MSDN Library - Invalid Syntax Error www.computing.net/answers/programming/msdn-library-invalid-syntax-error/12881.html

syntax error in QuickBasic or?????? www.computing.net/answers/programming/syntax-error-in-quickbasic-or/11224.html

awk syntax error www.computing.net/answers/programming/awk-syntax-error/19611.html