Visual Studio errors

Final Exam time: This is due Saturday night !!
I'm just a beginner, I compiled code in CodeBlocks with no errors,
I need to run this code in Visual Studio
There are 6 errors in lines 114,232,234,114,224,226




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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
#include<iostream>
#include<fstream>
#include<iomanip>

using namespace std;

//prototypes
int getTreeNo();
int getSpeciesCode(int[], string[]);
float getDbh();
int getTotHt();
double calcTotVol(int, int[], double[], double[], float, int);
void displayTreeData(ofstream&, int, int, int[], string[], double, int, double);

int main() {

	int treeNo, speciesCode, totalHt, noTrees;
	float dbh;
	double totalVol, avgTotVol;

	int Species[6] = { 11,12,13,21,22,23 };
	string speciesDesc[6] = { "Balsam Fir     ","Black Spruce   ","Red Pine       ","Trembling Ash  ","White Birch    ","Other Hardwoods" };
	double b0[6] = { 1.3686, 0.000, 2.1892, .8528, 1.7478, .7668 };
	double b1[6] = { .002182, .002468, .002052, .002251, .002142, .002164 };

	noTrees = 0;//initialized to 0
	avgTotVol = 0.0;//set volume to 0

	ofstream out("report.dat");//output is to be stored on report.dat external data file.

	cout << setiosflags(ios::fixed) << setprecision(3);
	out << setiosflags(ios::fixed) << setprecision(3);

	do /*major while control loop that allows the user to enter data
	     tree by tree until a tree number 999 is entered*/
        {
		treeNo = getTreeNo();
		if (treeNo != 999)
		{
			speciesCode = getSpeciesCode(Species, speciesDesc);
			dbh = getDbh();
			totalHt = getTotHt();
			totalVol = calcTotVol(speciesCode, Species, b0, b1, dbh, totalHt);
			noTrees++;//increment tree count
			avgTotVol += totalVol;//add current tree's totalVol to avgTotVol

			displayTreeData(out, speciesCode, treeNo, Species, speciesDesc, dbh, totalHt, totalVol);
		}
	} while (treeNo != 999);


	cout << "\n\nTotal trees measured = " << noTrees
		<< "\nAverage total volume = ";

	out << "\n\nTotal trees measured = " << noTrees
		<< "\nAverage total volume = ";

	if (noTrees < 1)
        {
		cout << 0;
		out << 0;
        }

	else
        {
		cout << avgTotVol / noTrees;
		out << avgTotVol / noTrees;
        }

	out.close();
	return 0;
}

/*
getTreeNo
Input tree number, input must be type int
and  > 0 and less than 150 or = 999.
A value of 999 terminates input.
*/
int getTreeNo()
{
	int input;
	do //Input is to be a do/while loop.
	{
		cout << "Enter tree number(between 1-199) or 999 to exit: ";
		cin >> input;
		if ((input < 1 || input > 199) && input != 999)
            {
			cout << "Invalid number entered.\n";
            }
	}
	while ((input < 1 || input > 199) && input != 999);

	return input; //Function is return by value.
}

/*
getSpeciesCode
Species code is type int and
must be valid and found in the species table.
*/
int getSpeciesCode(int Table[], string Name[])
{
	int speciesCode;
	int found = 0; //initialized to 0

	do //Input is a do/while loop.
	{
		//display Code & description
		cout << "\nspeciesCode\t\t\tDescription\n";

		for (int i = 0;i<6;i++)
            {
114			cout << Table[i] << "        " << Name[i] << endl;
            }

		//ask for input
		cout << "\nEnter Species Code: ";
		cin >> speciesCode;

		//search Table to see if valid speciesCode entered
		for (int c = 0;c<6;c++)
            {
			if (speciesCode == Table[c])
                {//if found,
				found = 1;
				break; //exit for loop
                }
            }

		if (found != 1)
            {
			cout << "Invalid species code entered.\n";
            }
	}
	while (found != 1);

	return speciesCode;//Function is return by value.
}

/*
getDbh
Values are type float and must be greater than
or equal to 5.0 and no greater than 50.6 inches.
*/

float  getDbh()
{
	double inches;
	do //Input is a do/while loop.
	{
		cout << "Enter the number of inches(between 5 and 50.6): ";
		cin >> inches;
		if (inches < 5 || inches > 50.6)
            {
			cout << "Invalid number entered.\n";
            }
	}
	while (inches <5 || inches > 50.6);

	return inches;//Function is return by value.

}

/*
getTotHt
Values are type int and can range from 22 to 180.
Total height is always measured as the closest even integer.
*/
int getTotHt()
{
	int Ht;//local alias
	do //Input is a do/while loop.
	{
		cout << "Enter the closest even total height(between 22 - 180): ";
		cin >> Ht;
		if ((Ht < 22 || Ht > 180) && Ht % 2 != 0)
            {
			cout << "Invalid number entered Note that it must be a even number.\n";
            }
	}
	while ((Ht < 22 || Ht > 180) && Ht % 2 != 0);

	return Ht;//Function is return by value.
}

/*
calcTotVol
Calculate total volume (type double) using supplied formula.
totalVol = b0 + (b1 * (dbh*dbh) * totalHt)
Output is 3 decimal places to the right of the decimal.
*/

double calcTotVol(int code, int table[], double b0[], double b1[], float dbh, int totalHt) {
	int index;
	double totalVol;

	//find index
	for (int i = 0;i<6;i++)
    {
		if (code == table[i])
		{
			index = i;
			break;
		}
	}

//calculate totalVol = b0 + (b1 * (dbh*dbh) * totalHt)
totalVol = b0[index] + (b1[index] * dbh * dbh * totalHt);

	return totalVol;//Return by value function.

}

/*

*/
void displayTreeData(ofstream& out, int speciesCode, int treeNo, int table[], string Name[], double dbh, int totalHt, double totalVol) {
	int index;

	//find index
	for (int i = 0; i < 6; i++)
        {
/*224*/		if (speciesCode == table[i])
            {
/*226*/			index = i;
			break;
            }
        }

cout << "\nTree No.        Species Description      DBH         Total Ht.     Total Vol.\n" << endl;
/*232*/   cout << treeNo << "               " << Name[index] << "        " << setprecision(1) << dbh << "           " << totalHt << "           " << setprecision(3) << totalVol << endl;
out << "\nTree No.        Species Description      DBH         Total Ht.     Total Vol.\n" << endl;
/*234*/   out << treeNo << "               " << Name[index] << "        " << setprecision(1) << dbh << "           " << totalHt << "           " << setprecision(3) << totalVol << endl;

	return;
}
What are the errors?
closed account (E0p9LyTq)
You failed to #include <string> .

Once that is taken care of all you have is a conversion warning.
Declare inches in float getDbh() a float instead of double and no more warnings.
closed account (E0p9LyTq)
helios wrote:
What are the errors?


Error (active)		no operator "<<" matches these operands	ConsoleApplication1	c:\Programming\My Projects\ConsoleApplication1\ConsoleApplication1\Source.cpp	232	
Error (active)		no operator "<<" matches these operands	ConsoleApplication1	c:\Programming\My Projects\ConsoleApplication1\ConsoleApplication1\Source.cpp	114	
Error (active)		no operator "<<" matches these operands	ConsoleApplication1	c:\Programming\My Projects\ConsoleApplication1\ConsoleApplication1\Source.cpp	234	
Error	C2679	binary '<<': no operator found which takes a right-hand operand of type 'std::string' (or there is no acceptable conversion)	ConsoleApplication1	c:\programming\my projects\consoleapplication1\consoleapplication1\source.cpp	114	
Error	C2679	binary '<<': no operator found which takes a right-hand operand of type 'std::string' (or there is no acceptable conversion)	ConsoleApplication1	c:\programming\my projects\consoleapplication1\consoleapplication1\source.cpp	232	
Error	C2679	binary '<<': no operator found which takes a right-hand operand of type 'std::string' (or there is no acceptable conversion)	ConsoleApplication1	c:\programming\my projects\consoleapplication1\consoleapplication1\source.cpp	234


All caused by failing to #include <string> .
I added #include <string>
I Declare inches in float getDbh() a float

I am still coming up with 3 errors

Error C2679 binary '<<': no operator found which takes a right-hand operand of type 'std::string' (or there is no acceptable conversion) ConsoleApplication1 c:\programming\my projects\consoleapplication1\consoleapplication1\source.cpp 114
Error C2679 binary '<<': no operator found which takes a right-hand operand of type 'std::string' (or there is no acceptable conversion) ConsoleApplication1 c:\programming\my projects\consoleapplication1\consoleapplication1\source.cpp 232
Error C2679 binary '<<': no operator found which takes a right-hand operand of type 'std::string' (or there is no acceptable conversion) ConsoleApplication1 c:\programming\my projects\consoleapplication1\consoleapplication1\source.cpp 234



Also I have a problem with this line of code

[code]
if ((Ht < 22 || Ht > 180) && Ht % 2 != 0)
[code]

it does accept an odd int
where did I go wrong?
closed account (E0p9LyTq)
I don't know what is happening. With VS 2015 Community I create a blank console project, add a blank source file, copy and paste your code.

I add #include <string> after the other includes::

1
2
3
4
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string> 


I rebuild the solution and I don't get any errors.
Hi,

Added #include <string>

using cpp.sh with all 3 warning level on, I get these:

In function 'double calcTotVol(int, int*, double*, double*, float, int)': 
195:6: warning: 'index' may be used uninitialized in this function [-Wmaybe-uninitialized] 
In function 'void displayTreeData(std::ofstream&, int, int, int*, std::string*, double, int, double)': 
232:60: warning: 'index' may be used uninitialized in this function [-Wmaybe-uninitialized] 
In function 'int main()': 
195:6: warning: 'index' may be used uninitialized in this function [-Wmaybe-uninitialized]


Always Initialising variables is a golden rule, In C++ one can delay the declaration of the variable until one has a sensible value to assign to it, so prefer to do that. Declaring more than 1 variable per line also is asking for trouble.

Prefer double rather than float, the precision of float is easily exceeded, and one can avoid the conversion warnings.

I routinely compile with all these warnings as well http://www.cplusplus.com/forum/general/183731/#msg899203 perhaps you can find the equivalent in VS, and turn them on? IIRC that might involve explicitly enabling those warning numbers.
Last edited on
closed account (E0p9LyTq)
TheIdeasMan wrote:
I routinely compile with all these warnings as well http://www.cplusplus.com/forum/general/183731/#msg899203 perhaps you can find the equivalent in VS, and turn them on? IIRC that might involve explicitly enabling those warning numbers.

With VS 2015 go to the Project's/Solution's property configuration page:

Configuration Properties -> C/C++ -> General -> Warning Level property.

You can set the properties for either Debug or Release builds, or for all configurations.

VS 2015 sets the /W3 warning level as default, setting /W4 level will give you all the warnings applicable to the current source code.

/Wall spits up a lot of C++ implementation warnings for the C++ std library and other Windows libraries; not something for a novice.

So, TheIdeasMan, thank you for making me hunt for the warning level configuration, and for reminding me about the float/double usage.
Last edited on
@FurryGuy

Hi,

Perhaps I should mention that I am a Linux guy - I use clang as a compiler, and I have only used the Express (2013) versions of VS briefly. I mentioned all those extra warnings for the OP because I find it works well for me - the more warnings one can enable the better :+) Well, under gcc or clang anyway.

From what I remember of VS: /Wall is a different thing to -Wall under gcc? And /W4 gives a tonne of fairly spurious warnings about system headers? Also, one can specify individual warnings to be turned by their number. I gather that /W3 doesn't report the same warnings as -Wall -Wextra -pedantic-errors under gcc, so this and the additional warnings I use was the motivation for suggesting to individually enable warnings.

The lack of warnings issued by VS is something I see all the time here on this forum: "My code compiles, then crashes (or has some runtime error)". So I compile their code with my settings and get a page full of warnings.

Anyway, I am sure you are all over this (way more experienced and knowledgeable than me).

Cheers :+)
> The lack of warnings issued by VS is something I see all the time

The default warning level in Visual Studio issues more warnings than the default with the GNU offering, and catches many non-conformance errors that g++ passes by default.

In general, with the Microsoft compiler, do not use the noisy -Wall (all warnings). It emits diagnostics foe everything - for instance: 'warning: call to function 'foo' (which was not marked inline) was auto inlined' etc..

A good combination for the Microsoft compiler is -W4 -analyze
The C/C++ Code Analysis tool provides information to developers about possible defects in their C/C++ source code. Common coding errors reported by the tool include buffer overruns, un-initialized memory, null pointer dereferences, and memory and resource leaks.
https://msdn.microsoft.com/en-us/library/d3bbz7tz.aspx



EDIT:

-analyze runs the C++ static analysis tool.

appverif is the dynamic analysis tool.
https://msdn.microsoft.com/en-us/library/windows/hardware/ff538115(v=vs.85).aspx
Last edited on
Thank you for all the info, I started a new project copied and pasted code in VS and it compiles and runs No Errors !! yeah thanks for all the help

This program is only supposed to accept Even numbers for Ht
Something is wrong in the do while loop PlEase Help

[code]
/*
getTotHt
Values are type int and can range from 22 to 180.
Total height is always measured as the closest even integer.
*/
int getTotHt()
{
int Ht;//local alias
do //Input is a do/while loop.
{
cout << "Enter the closest even total height(between 22 - 180): ";
cin >> Ht;
if ((Ht < 22 || Ht > 180) && Ht % 2 != 0)
{
cout << "Invalid number entered Note that it must be a even number.\n";
}
} while ((Ht < 22 || Ht > 180) && Ht % 2 != 0);

return Ht;//Function is return by value.
}
[code]

Any Suggestions are appreciated
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
bool is_even( int height ) { return height%2 == 0 ; }

bool is_valid( int height ) { return height > 21 && height < 181 && is_even(height) ; }

int getTotHt()
{
    int Ht;//local alias
    do //Input is a do/while loop.
    {
        cout << "Enter the closest even total height(between 22 - 180): ";
        cin >> Ht;
        if ( !is_valid(Ht) )
        {
            cout << "Invalid number entered Note that it must be a even number.\n";
        }
    }
    while ( !is_valid(Ht) ) ;

    return Ht;//Function is return by value.
}
I changed the && to || and its great
Thank you All for All your help I couldn't have done it without you !!
JLBorges wrote:
The default warning level in Visual Studio issues more warnings than the default with the GNU offering, and catches many non-conformance errors that g++ passes by default.


You are of course completely correct :+) It was unfair of me to single out VS, but perhaps it is more symptomatic of beginners being unaware of warning options at all - that seems exactly what happened in the OP. I wonder if the reason for the differences in the amount of warnings I mentioned might be due to a comparison between /W3 in VS and -Wall -Wextra -pedantic as the minimum recommended for gcc/clang?

I guess beginners find the learning curve steep enough as it is, warning options are just another thing on the list of many they might contend with in the future. Although recently I was somewhat appalled to hear of a tutor compiling with just g++ main.cpp

Also, this sort of thing could be a bit like using namespace std; , one could go on for quite awhile until they get burnt. For example, at the moment on the UNIX/Linux Programming forum there is a topic about threading. There is no mention of even -Wall at all, I wonder if that and other options were left out for brevity, or is it that OP and one of the respondents unaware? http://www.cplusplus.com/forum/unices/189160/
Topic archived. No new replies allowed.