strings as arguments in functions

I am getting this error when I create a function that attempts to use a string as an argument list than cout that string.
this particular function is designed to change a char variable used to repeat a program.
1
2
3
4
5
6
7
8
char program_Repeat(string a,char b)
{
         cout<<a<<endl;
     	cin>>repeat;
	if(repeat!=('y')||('Y')||('n')||('N'))
	{throw repeat_Variable_error();}
	return b;
}

Error 2 error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'std::string' (or there is no acceptable conversion)
it says I think that cout cannot accept std::string a. any ideas?
you can use string::c_str(), or try std::cout<<a<<std::endl;

#include <string>
closed account (Dy7SLyTq)
i think he did it or he would be getting an error invalid type string in the function argument list
thanks for the reply cire had it right. this is my code so far and well try and run it and see what happens it does not seem to be functioning correctly. it runs through the while loop without stopping and throws some exceptions that arent intended.. i am on windows 7 and visual studios 12
this is the main.cpp
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include<iostream>
#include<vector>
#include<algorithm>            //reverse function
#include<Windows.h>
#include"vector_Utilities.h"   //char_Input(vector.push_back routine)... vector_Iterator(print vector routine)... 
                               //reverse1(pass by value)... reverse2(pass by reference)
							   //elipses(sleep function and cout ".")
using namespace std;

int main()
{
	cout<<"Vector data reversal program.\n";
	char repeat='y';
	while(repeat==('y')||('Y'))
	{
		try
		{	
			vector<double> reversed, original;
			cout<<"Enter data to be reversed.\n";
			char_Input(original);
			if(original.size()==((0)||(1)))
			{ throw vector_Size_error();}
			cout<<"Calling reverse function by value";
			elipses();
			reversed=reverse1(original);
			vector_Iterator(reversed);
			cout<<"Original vector (unchanged).\n";
			vector_Iterator(original);
			Sleep(1000);
			cout<<"Calling reverse function by reference";
			elipses();
			reverse2(original);
			vector_Iterator(original);
			program_Repeat("Would you like to reverse more data? (Y/N).",repeat);
		}
		catch(vector_Size_error)
		{
			cout<<"Error: The size of the vector is either null or one and cannot be reversed.\n";
			program_Repeat("Would you like to try again?",repeat);
		}
		catch(repeat_Variable_error)
		{
			cout<<"Error: The request to reverse more data was not answered correctly.\n";
			program_Repeat("Type 'y' to restart or 'n' to terminate process.",repeat);
		}
	}
		return 0;
}

this is the .h file
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include<vector>

using namespace std;

class vector_Size_error{};
class repeat_Variable_error{};

vector <double> reverse1(vector <double> a);

void reverse2(vector <double>& a);

void char_Input(vector<double>& a);

void vector_Iterator(vector <double> a);

void elipses();

char program_Repeat(string a,char b);

and this is the .cpp file associated with the .h file
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include<iostream>
#include<vector>
#include<windows.h>
#include<string>
using namespace std;
//Utilities for vectors, while loop for data push_back. 
//reverse by value and by reference. character output iterator. 
class repeat_Variable_error{};
vector <double> reverse1(vector <double> a)
{
	reverse(a.begin(),a.end());
	return a;
}
//________________________________________
void reverse2(vector <double>& a)
{
	reverse(a.begin(),a.end());
}
//=============EXTRA==================
void char_Input(vector<double>& a)
{
	double input=0;
	while(cin>>input)
	{

		a.push_back(input);
	}
	
}
//__________________________________________
void vector_Iterator(vector <double> a)
{
	for(int i=0;i<a.size();++i)
	{
		cout<<a[i]<<" ";
	}
	cout<<endl;
}
//__________________________________________
void elipses()
{
	Sleep(1000);cout<<". ";Sleep(1000);cout<<". ";Sleep(1000);cout<<".\n";Sleep(1000);
}
//__________________________________________
char program_Repeat(string a,char b)
{
	cout<<a<<endl;
	char repeat=b;
	cin>>repeat;
	if(repeat!=('y')||('Y')||('n')||('N'))
	{throw repeat_Variable_error();}
	return b;
}
//================Credit==================== 
If you do this:
1
2
3
    char x = 'y' ;
    if ( x != ('y') || ('Y') )
        throw ;


The throw statement will always be executed, because what the compiler sees (according to operator precedence is:)

if ( (x != 'y') || 'Y' ) and since 'Y' is non-zero that expression will always evaluate to true.

So the correct way to write line 50 of your vector_Utilities.cpp is:

1
2
    if ( repeat != 'y' && repeat != 'Y' && repeat != 'n' && repeat != 'N' )
        throw repeat_Variable_error() ; 


Aside from that, there is no way for program_Repeat to affect the variable named repeat in your main function. Why do you pass in b?

You have the same sort of comparison problem in line 21 of your main function.

In char_Input you rely on std::cin entering an error state to terminate the input extraction loop, but you never clear the error state or remove the offending input, so all subsequent attempts to extract data from std::cin will fail.
Topic archived. No new replies allowed.