C++ String Arrays  | |
February 16th, 2007, 12:13 AM
|
#1 (permalink)
| | Ultimate Member
Join Date: May 2003 Location: Cheyenne, WY
Posts: 1,087
|
Is it possible to use an array for strings? I'll just post my code as it's easier to describe what I'm talking about. What this does is reads current, and average rainfall, then computes the variance. It then outputs it in a "table" style format.
The thing I can't get it to do is output the months. I made an array of type string but I get an error. I'm starting to think that I can't actually use a string array in the way that I'm trying. Anyone know another way to do this...or a way to correct what I have? Code: #include <iostream>
#include <iomanip>
#include <cmath>
#include <cctype>
#include <cstring>
#include <fstream>
using namespace std;
const int SIZE = 12;
int main()
{
double actual[SIZE], average[SIZE], variance[SIZE];
string month[SIZE]= {"Jan", "Feb", "Mar", "Apr", "May", "June", "July", "Aug", "Sep", "Oct", "Nov", "Dec"};
//open avg, and actual files that have input in them, and creating asg5output.txt for output
ifstream inavg, inact;
ofstream outf;
inavg.open("asg5average.txt");
inact.open("asg5rainfall.txt");
outf.open("asg5output.txt");
for (int i=0; i<SIZE; i++) //getting input into average and actual
{
inavg >> average[i];
inact >> actual[i];
}
//calculate variance
for (int i=0; i<SIZE; i++)
{
variance[i]=((actual[i]-average[i])/average[i])*100;
}
//output
cout<<"\tRainfall Comparison"<<endl<<endl;
cout<<"Month"<<"\tActual"<<"\tAverage"<<"\tVariance\n";
cout<<"_____"<<"\t______"<<"\t_______"<<"\t________\n";
for (int i=0; i<SIZE; i++)
{
cout<<noshowpos<<"\n"<<month[i]<<"\t"<<actual[i]<<"\t"<<average[i];
cout<<showpos<<"\t"<< variance[i];
}
cout<<endl<<endl;
inavg.close();
inact.close();
outf.close();
return 0;
}
__________________ Gi | Yuu | Jin | Rei | Makoto | Meiyo | Chuugi
Last edited by Vyx : February 16th, 2007 at 09:31 PM.
|
| |
February 16th, 2007, 06:09 PM
|
#2 (permalink)
| | Senior Member
Join Date: Oct 2002
Posts: 557
|
You know your missing the closing } on the main function? I compiled your code and it ran fine and printed the months. (though you might want to have some error handeling on opening your files, cuz i didnt have the files and it just grabbed random memory locations).
Other than that i dont see a problem. |
| |
February 16th, 2007, 09:31 PM
|
#3 (permalink)
| | Ultimate Member
Join Date: May 2003 Location: Cheyenne, WY
Posts: 1,087
|
Yea, I just accidnentally didn't copy the last closing brace.
I've attached the two text files that I was using.
The error I keep getting is: Error 1 error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'std::string' (or there is no acceptable conversion)
This is using Visual Studio 2005 Pro. What compiler were you using to compile it? |
| |
February 16th, 2007, 09:32 PM
|
#4 (permalink)
| | Real gangstas sip on Yacc
Join Date: Oct 2001 Location: Suckas-ville
Posts: 4,552
| Quote: |
I made an array of type string but I get an error.
| Just for future reference, it is much more helpful to post the actual compiler error than "this code causes an error".
Otherwise yes, it is possible to make an array of pretty much any object in C++.
Jkrohn
__________________
Signatures blow hard
If your signature contains an ad of any kind, congratulations, you're on my ignore list.
|
| |
February 16th, 2007, 10:02 PM
|
#5 (permalink)
| | Caveat Emptor
Join Date: Mar 2005 Location: Out of my mind
Posts: 3,241
|
Been a while since my C++ days...try casting it
cout << (string)month[i]... |
| |
February 17th, 2007, 02:52 AM
|
#6 (permalink)
| | Senior Member
Join Date: Oct 2002
Posts: 557
|
It worked fine in DevC++ using GCC 3.4.1 |
| |
February 19th, 2007, 01:06 AM
|
#7 (permalink)
| | Ultimate Member
Join Date: May 2003 Location: Cheyenne, WY
Posts: 1,087
|
Incase anyone looks over this in the future, the reason that it wasn't working was that I was using #include <cstring> when I should have just been using #include <string>. |
| |
February 19th, 2007, 09:41 AM
|
#8 (permalink)
| | Senior Member
Join Date: May 2003 Location: Aus, Gold Coast :)
Posts: 802
| |
| | | Thread Tools | Search this Thread | | | | |
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | | | | Most Active Discussions | | | | | Recent Discussions  | | | | | |