home hardware prices news articles forums photos user reviews
Go Back   Tech Support Forums - TechIMO.com > PC Hardware and Tech > Webmastering and Programming
Ask a Tech Support Question (free)!

C++ String Arrays

Reply
Get bargains at  »  Dealighted.com
 
Thread Tools Search this Thread
Currently Active Users: 2159
Discussions: 200,947, Posts: 2,379,379, Members: 246,307
Old February 16th, 2007, 12:13 AM   Digg it!   #1 (permalink)
Vyx
Ultimate Member
 
Vyx's Avatar
 
Join Date: May 2003
Location: Cheyenne, WY
Posts: 1,087
Send a message via AIM to Vyx Send a message via MSN to Vyx Send a message via Skype™ to Vyx
C++ String Arrays

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.
Vyx is offline   Reply With Quote
Old February 16th, 2007, 06:09 PM     #2 (permalink)
Senior Member
 
bfcx's Avatar
 
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.
bfcx is offline   Reply With Quote
Old February 16th, 2007, 09:31 PM     #3 (permalink)
Vyx
Ultimate Member
 
Vyx's Avatar
 
Join Date: May 2003
Location: Cheyenne, WY
Posts: 1,087
Send a message via AIM to Vyx Send a message via MSN to Vyx Send a message via Skype™ to Vyx
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?
Vyx is offline   Reply With Quote
Old February 16th, 2007, 09:32 PM     #4 (permalink)
Real gangstas sip on Yacc
 
jkrohn's Avatar
 
Join Date: Oct 2001
Location: Suckas-ville
Posts: 4,552
Send a message via ICQ to jkrohn Send a message via AIM to jkrohn Send a message via Yahoo to jkrohn
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.
jkrohn is offline   Reply With Quote
Old February 16th, 2007, 10:02 PM     #5 (permalink)
Caveat Emptor
 
Rootstonian's Avatar
 
Join Date: Mar 2005
Location: Out of my mind
Posts: 3,241
Send a message via AIM to Rootstonian
Been a while since my C++ days...try casting it

cout << (string)month[i]...
Rootstonian is offline   Reply With Quote
Old February 17th, 2007, 02:52 AM     #6 (permalink)
Senior Member
 
bfcx's Avatar
 
Join Date: Oct 2002
Posts: 557
It worked fine in DevC++ using GCC 3.4.1
bfcx is offline   Reply With Quote
Old February 19th, 2007, 01:06 AM     #7 (permalink)
Vyx
Ultimate Member
 
Vyx's Avatar
 
Join Date: May 2003
Location: Cheyenne, WY
Posts: 1,087
Send a message via AIM to Vyx Send a message via MSN to Vyx Send a message via Skype™ to Vyx
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>.
Vyx is offline   Reply With Quote
Old February 19th, 2007, 09:41 AM     #8 (permalink)
Senior Member
 
Join Date: May 2003
Location: Aus, Gold Coast :)
Posts: 802
Send a message via ICQ to exally
char* ftw
exally is offline   Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search

Similar Threads
Thread Thread Starter Forum Replies Last Post
can arrays count for you? ShuckyD Webmastering and Programming 9 December 5th, 2006 06:51 AM
overclocking and raid arrays mrniceguy Processors, Memory, and Overclocking 2 October 3rd, 2006 12:56 PM
Fast RAID arrays andylister Storage Related 5 April 2nd, 2004 05:41 AM
Access ACTextBox/Arrays ??? Turnip12 Webmastering and Programming 2 October 22nd, 2003 08:13 PM
Need help with multidim arrays/pointers skybolt_1 Webmastering and Programming 5 May 1st, 2003 07:04 AM


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Most Active Discussions
Making Health Care Worse (167)
The disrespect of Obama by Russian .. (19)
Is It Just Me? (2932)
Wireless Televisions. (11)
windows 7 problem (7)
CPU fan stops spinning randomly (8)
Regular Build (6)
Is the PSU I received dead? (12)
radeon x850xt platinum & shader.. (5)
Print spooler problem (15)
HIS HD5770 graphic card question (15)
windows vista security holes (9)
Install XP pro and a Vista laptop ?.. (11)
Foreign voltage (10)
Recent Discussions
Internet Lost (0)
Graphics Card Upgrade Questions (0)
Graphics Card Upgrade Question (2)
Asus P4G8X Mobo (6)
radeon x850xt platinum & shader 3 (5)
Xbox 360 GTA: SA disk error (1)
Is the PSU I received dead? (12)
How to convert MP3's (2)
windows 7 internet problem (5)
Multiple Restarts Required at Boot (0)
BSOD On Startup (ntoskrnl.exe) (2)
Print spooler problem (15)
Laptop with wireless problem. (1)
Wireless Televisions. (11)
Have you switched yet? (86)
screen resolution vs monitor size (2)
sms storage to PC (0)
Regular Build (6)
Open With ..... Win7 (0)
java code for fibonacci (1)
[F@H SPAM 11/16/09] ! 1/2 months to r.. (35)
windows 7 problem (7)
CPU fan stops spinning randomly (8)
Partition Magic caused HDD problem (3)
Point and Shoot Camera Suggestions. (2)


All times are GMT -4. The time now is 07:30 PM.
TechIMO Copyright 2009 All Enthusiast, Inc.



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28