1 Replies - 854 Views - Last Post: 26 April 2008 - 11:34 AM Rate Topic: -----

#1 Bobby B  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 26-April 08

Passing string to function

Post icon  Posted 26 April 2008 - 08:01 AM

Hi,

I am suppose to write a program for resistors that takes colors from a file, assigns the appropriate
value to it, and then calculates the total resistance value.

The main problem is that the I have to send four string to a function, and then that function is suppose to
send back two 1-D arrays.

//Name:
//Date: 4/21/08
//Assignment: Programming Assignment #3, write a program that calculates
//resistance values from text files.
/******************************************************************************/

#include <iostream>
#include <cmath>
#include <string>
#include <iomanip>
#include <fstream>

using namespace std;

//array size declared in global.
const int N = 30;

string StringToLower(string);
string Values_of_R(string&, string&, string&, string&);

int main ()
{
	unsigned int i;
	string BandA[N], BandB[N], BandC[N], BandD[N], BandA_lower[N], 
		   BandB_lower[N], BandC_lower[N], BandD_lower[N];
   
		   
	//string arrays used for input and output filenames.	   
	string input_filename, output_filename;	   
	
	cout << "Please enter the input filename (with path and drive): ";
	cin >> input_filename;
	
	cout << "Please enter the output filename (with path and drive): ";
	cin >> output_filename;
	
	ifstream infile (input_filename.c_str());
	ofstream outfile (output_filename.c_str());
	
	//for loop with getline function to read data from the files.
	for (i = 0; i < 25; i++)
	{
		getline (infile, BandA[i], ' ');
		getline (infile, BandB[i], ' ');
		getline (infile, BandC[i], ' ');
		getline (infile, BandD[i]);
	}
	
	
	
	//for loop used to convert data in lower cases.
	for (i = 0; i < 25; i++)
	{
		BandA_lower[i] = StringToLower(BandA[i]);
		BandB_lower[i] = StringToLower(BandB[i]);
		BandC_lower[i] = StringToLower(BandC[i]);
		BandD_lower[i] = StringToLower(BandD[i]);
	}
	
	
	//for loop used to send lowercased arrays to Values_of_R function.
	for (i = 0; i < 25; i++)
	{
		Values_of_R(BandA_lower[i], BandB_lower[i], BandC_lower[i],
									BandD_lower[i]);
	}
	
	
	system("pause");
	return 0;
}


//Function used to convert data to lower case.	
string StringToLower(string strToConvert)
{//change each element of the string to lower case
   for(unsigned int i=0;i<strToConvert.length();i++)
   {		
	  strToConvert[i] = tolower(strToConvert[i]);
   }
   return strToConvert;//return the converted string
}	
   
   
   
string Value_of_R(string& BandA_lower, string& BandB_lower, string& BandC_lower,
		string& BandD_lower)
{
   
   double Resistance_Values_A[N], Resistance_Values_B[N],
   Resistance_Values_C[N], Resistance_Values[N], Tolerance_Values[N];
 
		
   //for loop used to calculate resistance values for Band A.
   for (int i = 0; i < 25; i++)
   {
		   if (BandA_lower[i] == "black")
			  Resistance_Values_A[i] = -1.0;
		   else if (BandA_lower[i] == "gold")
				Resistance_Values_A[i] = -1.0;
		   else if (BandA_lower[i] == "silver")
				Resistance_Values_A[i] = -1.0;
		   else if (BandA_lower[i] == "brown")
				Resistance_Values_A[i] = 1;
		   else if (BandA_lower[i] == "red")
				Resistance_Values_A[i] = 2;
		   else if (BandA_lower[i] == "orange")
				Resistance_Values_A[i] = 3.0;
		   else if (BandA_lower[i] == "yellow")
				Resistance_Values_A[i] = 4.0;
		   else if (BandA_lower[i] == "green")
				Resistance_Values_A[i] = 5.0;
		   else if (BandA_lower[i] == "blue")
				Resistance_Values_A[i] = 6.0;
		   else if (BandA_lower[i] == "violet")
				Resistance_Values_A[i] = 7.0;
		   else if (BandA_lower[i] == "gray")
				Resistance_Values_A[i] = 8.0;
		   else if (BandA_lower[i] == "white")
				Resistance_Values_A[i] = 9.0;
	}
	 
	for (i = 0; i < 25; i++)
	{	   
		   //if-else statement now goes to Band B.
		   if (BandB_lower[i] == "black")
			  Resistance_Values_B[i] = 0.0;
		   else if (BandB_lower[i] == "gold")
				Resistance_Values_B[i] = -1.0;
		   else if (BandB_lower[i] == "silver")
				Resistance_Values_B[i] = -1.0;
		   else if (BandB_lower[i] == "brown")
				Resistance_Values_B[i] = 1.0;
		   else if (BandB_lower[i] == "red")
				Resistance_Values_B[i] = 2.0;
		   else if (BandB_lower[i] == "orange")
				Resistance_Values_B[i] = 3.0;
		   else if (BandB_lower[i] == "yellow")
				Resistance_Values_B[i] = 4.0;
		   else if (BandB_lower[i] == "green")
				Resistance_Values_B[i] = 5.0;
		   else if (BandB_lower[i] == "blue")
				Resistance_Values_B[i] = 6.0;
		   else if (BandB_lower[i] == "violet")
				Resistance_Values_B[i] = 7.0;
		   else if (BandB_lower[i] == "gray")
				Resistance_Values_B[i] = 8.0;
		   else if (BandB_lower[i] == "white")
				Resistance_Values_B[i] = 9.0;
	}		
	
	for (i = 0; i < 25; i++)
	{   
		   //if-else statement now goes to Band C.		 
		   if (BandC_lower[i] == "black")
			  Resistance_Values_C[i] = 0.0;
		   else if (BandC_lower[i] == "gold")
				Resistance_Values_C[i] = -1.0;
		   else if (BandC_lower[i] == "silver")
				Resistance_Values_C[i] = -2.0;
		   else if (BandC_lower[i] == "brown")
				Resistance_Values_C[i] = 1.0;
		   else if (BandC_lower[i] == "red")
				Resistance_Values_C[i] = 2.0;
		   else if (BandC_lower[i] == "orange")
				Resistance_Values_C[i] = 3.0;
		   else if (BandC_lower[i] == "yellow")
				Resistance_Values_C[i] = 4.0;
		   else if (BandC_lower[i] == "green")
				Resistance_Values_C[i] = 5.0;
		   else if (BandC_lower[i] == "blue")
				Resistance_Values_C[i] = 6.0;
		   else if (BandC_lower[i] == "violet")
				Resistance_Values_C[i] = 7.0;
		   else if (BandC_lower[i] == "gray")
				Resistance_Values_C[i] = 8.0;
		   else if (BandC_lower[i] == "white")
				Resistance_Values_C[i] = 9.0;
	}
				
	
	for(i = 0; i < 25; i++)
	{				  
		   //if-else statement now goes to Band D.
		   if (BandD_lower[i] == "gold")
			  Tolerance_Values[i] = 5.0;
		   else if (BandD_lower[i] == "silver")
				Tolerance_Values[i] = 10.0;
		   else if (BandD_lower[i] == "none")
				Tolerance_Values[i] = 20.0;
	   }
}
 



This is the error I get:
97 ISO C++ forbids comparison between pointer and integer

If I do this comparsion without passing it to the function, the program works fine and the right values are assigned to their respective colors.

Any help will be greatly apperciated.

Is This A Good Question/Topic? 0
  • +

Replies To: Passing string to function

#2 Pontus  Icon User is offline

  • Cattlebruiser

Reputation: 17
  • View blog
  • Posts: 612
  • Joined: 28-December 06

Re: Passing string to function

Posted 26 April 2008 - 11:34 AM

Well, i think u try think that string& BandA_lower is a array, this is not an array. Try to replace it with string *BandA_lower.

Hope this helps
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1