Cin operator error! WHY?

Totally lost! I'm getting an error on the cin operator, and can't be able to define why. I'm getting this as the rror message
 while trying to match the argument list '(std::ifstream, std::string [])'
1>c:\users\pc\desktop\c++ programming\programs\election program\election program\election program.cpp(41): fatal error C1903: unable to recover from previous error(s); stopping compilation
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========


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
  
#include "stdafx.h"
#include <conio.h>
#include <iostream>
using namespace std;
#include <fstream>
#include <string>;
#include <cstring>

void display()
{
	cout<<"Allow the user to enter the last names of five canidates in a local election. \nType in number of votes. Display the percentage.\n\n";

	

}

void open_makefile (string & filename, string & filename2, ifstream &in, ofstream& out)
{
	//Make input file
	cout<<"Make a file for input: ";
	cin>>filename;
	//open input file
	in.open(filename.c_str());

	//open output file
	cout<<"\nMake a file for output: ";
	cin>>filename2;

	out.open(filename2.c_str());



}

void performwhileloop( ifstream & input, ofstream &output, string candidate[],int size,int vote[],int size2)
{
	candidate[size];
	vote[size2];
	while (input>>candidate>>vote)




}
I have an error on line 40 in the while loop input>> I even tried finishing my while loop yet I stil get an error which I cannot identiy. :(
Something is wrong with your performWhileLoop function parameter list. Double check this, also not 100% on this... But I would not recommend setting up a while loop in that way, that could be the issue also.

Your function naming convention is strange. It's good practice to stick with either
1)CamelCase
or
2)using_Underscores


but not both.
Thank you parasin for your response.! I will keep that In my reference. Now, this is totally weird. I added an "I" and the error disappeared. Rather then beign amused about it, may you or any forum member explain to me what the problem was?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
void performwhileloop( ifstream & input, ofstream &output, string candidate[],int size,int vote[],int size2)
{
	//size will be 5 for both of them
	int i=0;
	candidate[size];
	vote[size2];
	while (input>>candidate[i]>>vote[i] && i<size)
	{
		output<<candidate[i] << " "<< vote[i];
	
	
	
	}




}
your "candidate" variable and your "vote" variable are strings, which are arrays of characters, you passed these variables to your function as an array. Therefore, you have to use index notation in order to access the variables..
Last edited on
Yes, I was thinking the same thing until I came across my previous assignment. I'm using an array here to but in this case I didn't had to use an index?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
void ReadStudentid_TestAnswers(ifstream &infile, ofstream& outfile,char studentid[], int idsize,char answers[],int answersize)
{
	studentid[idsize];
	answers[answersize];
	while (infile>>studentid>>answers)
	{
	//Read data into infile 
	
	
	

	//output data into outfile
	outfile<<"\nID: ";
	outfile<<studentid;
just a correction, vote is a int data type not string :)
in case it's not clear :
string foo[ ] is different from char foo[]

the former is an array of string, and a string is an array of char while the latter is just an array of char,
in the former, you can store multiple words and access it using foo[ index ]

while in char foo[] you can only store a word, and if you access it using
foo[ index ], what you will access is a single character

1
2
3
4
5
6
7
8
9
string foo // a string or array of char, logically similar to char foo[]
char foo[] // logically equal to the above

string foo[] // this is not equal to the other 2 above, this can be think of as an array of array of char
//--
char* foo[]
char foo[][]
char** foo
// are all logically equivalent to string foo[] 


Edit BTW, in your performwhileloop() function, you don't have any statement that increments i, and also you redefine candidate and vote w/c is not needed, so your function should be something like :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void performwhileloop(
            ifstream& input,
            ofstream& output,
            string candidate[],
            int size, int vote[],int size2
            )
{
	int i=0;

	while ( input >> candidate[ i ] >> vote[ i ] || i < size )
	{
		output << candidate[ i ] << " " << vote[ i ]; // i think you are missing a newline
                i++; 
	}
}


and also, just like what Parasin said, you should give a function a reasonable and straightforward name, something like writeInfoToFile(), writeVotesToFile() etc..
Last edited on
Topic archived. No new replies allowed.