always getting zero on my counter variable

I don't seem to understand what I'm doing wrong. My guess is that somehow my string is not being read from the input file. When I complie and run this code I get "o" always for "int total_Cow" Someone please guide me or explain to me what I am doing wrong and potentially how to fix it. Thanks in advance. Also my input file looks like this below

(()()()()()()()))(())(((()))()((((()()((((()(((()))))()(

basically bunchof parenthesis with no spaces...

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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
  //make a file for input
void make_input(ifstream &fin,string &filename)

{
	cout<<"Input File: ";
	getline(cin,filename);

	//Open the file
	fin.open(filename);
		//if file doesnt open
			if(!fin)
				cout<<"\nFile not open";
}

void make_output(ofstream &fout,string &filename2)
{
	cout<<"\n\tOutput File Name: ";
	getline(cin,filename2);

	//Open the output file
	fout.open(filename2);

}

//input cow in file
void Put_cow(string &findcow, ifstream &in)
{
	getline(in,findcow);
}


void Read_File(string cow, int size, int  &counter)
{
	int rightcow=0;
	counter=0;
	for (int i=1;i<=size;i++)
	{
		if (cow[i-1]==')' &&  cow[i]==')')
		{

			counter=counter+rightcow;
		}
		else if (cow[i-1]=='(' && cow[i]=='(')
		
			rightcow=1;
	}

	
			

}


int _tmain(int argc, _TCHAR* argv[])
{
	cout<<"Hello";
	cout.flush();
	//Declare variables for ifstream and ofstream
		ifstream in;
		ofstream out;
		string name_Input_File;
		string name_Output_File;

		string find_cow;
		int length;

	//Call the function to open input file
		make_input(in,name_Input_File);
	//Call funtion to open output file
		make_output(out,name_Output_File);

		//read the file while file is open
		Put_cow(find_cow,in);
		//Find the length of string
		length=find_cow.length();

		//Total posilbity of cow
		int total_Cow;
		
		
		Read_File(find_cow,length,total_Cow);
		cout<<"\n\n\t" <<total_Cow;
		
Check to make sure that this condition:

1
2
if (cow[i-1]=='(' && cow[i]=='(')
	rightcow=1;


Is being met, otherwise you will always get zero

Also look out for this condition:
for (int i=1;i <= size;i++)
Smac, I did what you told me to but while working on this I seem to realize that my string is not being read from the input file, in my opinion. In my void put_cow function, I just want the user to enter in data to that input file and then display it on console. All I get on my console is an empty string. On the other hand, in void put_cow function I used cout<<"hello" and that did seem to print out the message on console. I can't understand why my input file is not being read...please be willing to help me out with this :) Thanks! :)

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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
//make a file for input
void make_input(ifstream &fin,string &filename)

{
	cout<<"Input File: ";
	getline(cin,filename);

	//Open the file
	fin.open(filename);
		//if file doesnt open
			if(!fin)
				cout<<"\nFile not open";
}

void make_output(ofstream &fout,string &filename2)
{
	cout<<"\n\tOutput File Name: ";
	getline(cin,filename2);

	//Open the output file
	fout.open(filename2);

}

//input cow in file
void Put_cow(string &findcow, ifstream &in)
{
	getline(in,findcow);
	cout<<findcow;
}

void count_cowlegs(string cow_str)
{
	int counter=0;
	int comb=0;
	//Find length of string
	string::size_type length=cow_str.length();

			for(int i=1;i<length;i++)
				{
					if (cow_str[i]=='(' && cow_str[i+1]=='(')
						{
							counter=comb+counter;
						}
					else if (cow_str[i]=='(' && cow_str[i+1]=='(')
						comb=1;
	
				}

}


int _tmain(int argc, _TCHAR* argv[])
{
	cout<<"Hello";
	cout.flush();
	//Declare variables for ifstream and ofstream
		ifstream in;
		ofstream out;
		string name_Input_File;
		string name_Output_File;

		string find_cow;
		

	//Call the function to open input file
		make_input(in,name_Input_File);
	//Call funtion to open output file
		make_output(out,name_Output_File);

		//read the file while file is open
		Put_cow(find_cow,in);
		

		
		
		
There is only on thing I can think of. The text in your input file does not begin on the first line which is why getline did not find anything when it read the first line.

Make sure there is some input on the first line of the file or post the input file here for us to take a look at
Did check the first line of my my input line and its all valid for my code

starting from line one, this is my input file
()((())()(()))()()((())())())())(())(())



Your code works for me:

1
2
3
4
5
 ./cow
HelloInput File: in

	Output File Name: out
()((())()(()))()()((())())())())(())(())
smac89, I'm doing the same thing and on my console output this is what I typed and got in return...

1
2
3
4
5
6
7
./cow
HelloInput File: hello.txt

             Output File Name: cowfind.txt
_(cursor blinking here)



Not sure whats exactly wrong then, but do you think it has to do something with my visual studio or compiler
It might be that visual studio has made your program to be line-buffered. So you need to print a newline or flush the buffer (line you did on line 56) in order to see the string that needs to be displayed.

try this:
28
29
...
std::cout << findcow << std::endl;
Topic archived. No new replies allowed.