Jump to content

C++ Program Help

Go to solution Solved by ThreePinkApples,

Try changing the name of count, there could be something in std that uses the same name, therefore it is ambiguous

I am trying to make a program that will let one user enter a name and the other player no knowing the name will hav to guess it. I have written this program before, but in a different language. I am getting the errors saying my variables are ambiguous and that the operators I'm using don't exist. Thanks for the help, I don't think it should be too hard to solve.

#include <iostream>using namespace std;string name;string guess;int count = 0;int main() {	cout << "Enter a name for the other player to guess";	cin >> name;	system("cls");	while (count < 10) {		cout << "Enter your guess for the name";		cin >> guess;		if (guess == name) {			cout << "Congratulations you guessed the name, without cheating...!";			break;		}		else {			count ++;		}	}	return 0;}
Link to comment
https://linustechtips.com/topic/206395-c-program-help/
Share on other sites

Link to post
Share on other sites

If I leave out the system("cls"); line, it compiles fine for me and

works (compiler: g++, using Linux)

BUILD LOGS: HELIOS - Latest Update: 2015-SEP-06 ::: ZEUS - BOTW 2013-JUN-28 ::: APOLLO - Complete: 2014-MAY-10
OTHER STUFF: Cable Lacing Tutorial ::: What Is ZFS? ::: mincss Primer ::: LSI RAID Card Flashing Tutorial
FORUM INFO: Community Standards ::: The Moderating Team ::: 10TB+ Storage Showoff Topic

Link to comment
https://linustechtips.com/topic/206395-c-program-help/#findComment-2801154
Share on other sites

Link to post
Share on other sites

If I leave out the system("cls"); line, it compiles fine for me and

works (compiler: g++, using Linux)

Damnit Linux! Always being better than windows. I'm using VS Express and I'm still getting errors without that line :(

 

Errors:

Error	1	error C2679: binary '>>' : no operator found which takes a right-hand operand of type 'std::string' (or there is no acceptable conversion)	c:\users\mike\documents\visual studio 2013\projects\guessing game\guessing game\source.cpp	11	1	Guessing GameError	2	error C2872: 'count' : ambiguous symbol	c:\users\mike\documents\visual studio 2013\projects\guessing game\guessing game\source.cpp	12	1	Guessing GameError	3	error C2679: binary '>>' : no operator found which takes a right-hand operand of type 'std::string' (or there is no acceptable conversion)	c:\users\mike\documents\visual studio 2013\projects\guessing game\guessing game\source.cpp	14	1	Guessing GameError	4	error C2872: 'count' : ambiguous symbol	c:\users\mike\documents\visual studio 2013\projects\guessing game\guessing game\source.cpp	20	1	Guessing Game	5	IntelliSense: no operator ">>" matches these operands            operand types are: std::istream >> std::string	c:\Users\Mike\Documents\Visual Studio 2013\Projects\Guessing Game\Guessing Game\Source.cpp	11	6	Guessing Game	6	IntelliSense: "count" is ambiguous	c:\Users\Mike\Documents\Visual Studio 2013\Projects\Guessing Game\Guessing Game\Source.cpp	12	9	Guessing Game	7	IntelliSense: no operator ">>" matches these operands            operand types are: std::istream >> std::string	c:\Users\Mike\Documents\Visual Studio 2013\Projects\Guessing Game\Guessing Game\Source.cpp	14	7	Guessing Game	8	IntelliSense: "count" is ambiguous	c:\Users\Mike\Documents\Visual Studio 2013\Projects\Guessing Game\Guessing Game\Source.cpp	20	4	Guessing Game
Link to comment
https://linustechtips.com/topic/206395-c-program-help/#findComment-2801202
Share on other sites

Link to post
Share on other sites

Try a cin.ignore() after you have cleared the screen. No guarantee it will work, I just have a suspicion

 

EDIT: OH, you need #include <string> (it helped alot when you posted the errors )

Ryzen 7 5800X     Corsair H115i Platinum     ASUS ROG Crosshair VIII Hero (Wi-Fi)     G.Skill Trident Z 3600CL16 (@3800MHzCL16 and other tweaked timings)     

MSI RTX 3080 Gaming X Trio    Corsair HX850     WD Black SN850 1TB     Samsung 970 EVO Plus 1TB     Samsung 840 EVO 500GB     Acer XB271HU 27" 1440p 165hz G-Sync     ASUS ProArt PA278QV     LG C8 55"     Phanteks Enthoo Evolv X Glass     Logitech G915      Logitech MX Vertical      Steelseries Arctis 7 Wireless 2019      Windows 10 Pro x64

Link to comment
https://linustechtips.com/topic/206395-c-program-help/#findComment-2801203
Share on other sites

Link to post
Share on other sites

Try a cin.ignore() after you have cleared the screen. No guarantee it will work, I just have a suspicion

 

EDIT: OH, you need #include <string> (it helped alot when you posted the errors )

Thanks that removed a few errors, count being ambiguous however remains.

 

Errors:

Error	1	error C2872: 'count' : ambiguous symbol	c:\users\mike\documents\visual studio 2013\projects\guessing game\guessing game\source.cpp	13	1	Guessing GameError	2	error C2872: 'count' : ambiguous symbol	c:\users\mike\documents\visual studio 2013\projects\guessing game\guessing game\source.cpp	21	1	Guessing Game	3	IntelliSense: "count" is ambiguous	c:\Users\Mike\Documents\Visual Studio 2013\Projects\Guessing Game\Guessing Game\Source.cpp	13	9	Guessing Game	4	IntelliSense: "count" is ambiguous	c:\Users\Mike\Documents\Visual Studio 2013\Projects\Guessing Game\Guessing Game\Source.cpp	21	4	Guessing Game
Link to comment
https://linustechtips.com/topic/206395-c-program-help/#findComment-2801233
Share on other sites

Link to post
Share on other sites

Try changing the name of count, there could be something in std that uses the same name, therefore it is ambiguous

Ryzen 7 5800X     Corsair H115i Platinum     ASUS ROG Crosshair VIII Hero (Wi-Fi)     G.Skill Trident Z 3600CL16 (@3800MHzCL16 and other tweaked timings)     

MSI RTX 3080 Gaming X Trio    Corsair HX850     WD Black SN850 1TB     Samsung 970 EVO Plus 1TB     Samsung 840 EVO 500GB     Acer XB271HU 27" 1440p 165hz G-Sync     ASUS ProArt PA278QV     LG C8 55"     Phanteks Enthoo Evolv X Glass     Logitech G915      Logitech MX Vertical      Steelseries Arctis 7 Wireless 2019      Windows 10 Pro x64

Link to comment
https://linustechtips.com/topic/206395-c-program-help/#findComment-2801251
Share on other sites

Link to post
Share on other sites

Try changing the name of count, there could be something in std that uses the same name, therefore it is ambiguous

Thanks I wouldn't have though about changing the variables name. I guess that is used as a variable in Visual Studio or Windows. Thanks again :)

 

Now I am a total C++ master!

Link to comment
https://linustechtips.com/topic/206395-c-program-help/#findComment-2801338
Share on other sites

Link to post
Share on other sites

Started C++ two weeks ago myself, and I love it : D

Ryzen 7 5800X     Corsair H115i Platinum     ASUS ROG Crosshair VIII Hero (Wi-Fi)     G.Skill Trident Z 3600CL16 (@3800MHzCL16 and other tweaked timings)     

MSI RTX 3080 Gaming X Trio    Corsair HX850     WD Black SN850 1TB     Samsung 970 EVO Plus 1TB     Samsung 840 EVO 500GB     Acer XB271HU 27" 1440p 165hz G-Sync     ASUS ProArt PA278QV     LG C8 55"     Phanteks Enthoo Evolv X Glass     Logitech G915      Logitech MX Vertical      Steelseries Arctis 7 Wireless 2019      Windows 10 Pro x64

Link to comment
https://linustechtips.com/topic/206395-c-program-help/#findComment-2801344
Share on other sites

Link to post
Share on other sites

Oh, I am attending an University here in Norway and the C++ class is this semester (5. semester of Computer Engineering). 

 

I have just made some small simple programs as hand-ins for the class. Also started working on Project Euler in C++ just to get a little more feeling about it

 

I don't really know precisely what I like about it, it might just be me sick and tired of C# (had that last semester) :P

Ryzen 7 5800X     Corsair H115i Platinum     ASUS ROG Crosshair VIII Hero (Wi-Fi)     G.Skill Trident Z 3600CL16 (@3800MHzCL16 and other tweaked timings)     

MSI RTX 3080 Gaming X Trio    Corsair HX850     WD Black SN850 1TB     Samsung 970 EVO Plus 1TB     Samsung 840 EVO 500GB     Acer XB271HU 27" 1440p 165hz G-Sync     ASUS ProArt PA278QV     LG C8 55"     Phanteks Enthoo Evolv X Glass     Logitech G915      Logitech MX Vertical      Steelseries Arctis 7 Wireless 2019      Windows 10 Pro x64

Link to comment
https://linustechtips.com/topic/206395-c-program-help/#findComment-2801432
Share on other sites

Link to post
Share on other sites

Oh, I am attending an University here in Norway and the C++ class is this semester (5. semester of Computer Engineering). 

 

I have just made some small simple programs as hand-ins for the class. Also started working on Project Euler in C++ just to get a little more feeling about it

 

I don't really know precisely what I like about it, it might just be me sick and tired of C# (had that last semester) :P

Cool, I'm planning to do computer sciences as well, but in Canada, even though I'd love to have the internet speed in norway :P

I have bookmarked Project Euler as well :)

Ya I used to use Turing an old language they taught in my highschool and it wasn't good enough so I have now moved onto bigger and better things. finally

Link to comment
https://linustechtips.com/topic/206395-c-program-help/#findComment-2801500
Share on other sites

Link to post
Share on other sites

#include <iostream>using namespace std;int main() {    string name;string guess;int count = 0;	cout << "Enter a name for the other player to guess";	cin >> name;	while (count < 10) {		cout << "Enter your guess for the name";		cin >> guess;		if (guess == name) {			cout << "Congratulations you guessed the name, without cheating...!";			break;		}		else {			count ++;		}	}	return 0;}

dont define variables outside of the main function. when i put the var's in the main function it compiled fine. EDIT: removing the system("cls"); fixed the problem. i compiled using code blocks so yeaa it was on windows

CPU: i7 4770k@4.3Ghz GPU: GTX 780ti Motherboard: ROG maximus vi formula PSU: Evga supernova 1000w Platinum

Case: NZXT Switch 810 Memory: g.skill ripjaws X Cooler: Corsair h100i(getting custom loop when i get money next) Storage: Samsung 840 evo 250gb Keyboard: Corsair K95 Mouse: SteelSeries Rival

Link to comment
https://linustechtips.com/topic/206395-c-program-help/#findComment-2801558
Share on other sites

Link to post
Share on other sites

#include <iostream>using namespace std;int main() {    string name;string guess;int count = 0;	cout << "Enter a name for the other player to guess";	cin >> name;	while (count < 10) {		cout << "Enter your guess for the name";		cin >> guess;		if (guess == name) {			cout << "Congratulations you guessed the name, without cheating...!";			break;		}		else {			count ++;		}	}	return 0;}

dont define variables outside of the main function. when i put the var's in the main function it compiled fine. EDIT: removing the system("cls"); fixed the problem. i compiled using code blocks so yeaa it was on windows

 

Oh thanks I didn't know that.

Link to comment
https://linustechtips.com/topic/206395-c-program-help/#findComment-2801614
Share on other sites

Link to post
Share on other sites

-snip-

this is because of variable shadowing: if there are two scopes, the first containing the second, each scope can have its own definition of a variable with the same name. if you name the variable, you're referring to the inner-most you can find in the current scope

 

example:

#include <iostream> int main(){          int i = 1;          {                    int i = 3;                    std::cout << "this is going to be 3: " << i << std::endl;                    i = 10; // this vale is writte to the inner-most i, which is just about to be destroyed because the scope is ending          }           std::cout << "this is going to be 1: " << i << std::endl;}

speaking about your code:

using namespace std; // import everything from std:: into the current scope. including std::count (http://www.cplusplus.com/reference/algorithm/count/) which is now accessible via its name, without namespace prefix int count = 0; // count already exists in the current scope, so creating a new one is ambiguous int main(){          count ++; // which one?}
using namespace std; // import std:: in this outer scope int main(){          int count = 0; // shadowing: count is declared in a different scope (the scope of main())           count ++; // refers to the inner-most declaration of count (the one local to main())}
Link to comment
https://linustechtips.com/topic/206395-c-program-help/#findComment-2801966
Share on other sites

Link to post
Share on other sites

 

this is because of variable shadowing: if there are two scopes, the first containing the second, each scope can have its own definition of a variable with the same name. if you name the variable, you're referring to the inner-most you can find in the current scope

 

example:

#include <iostream> int main(){          int i = 1;          {                    int i = 3;                    std::cout << "this is going to be 3: " << i << std::endl;                    i = 10; // this vale is writte to the inner-most i, which is just about to be destroyed because the scope is ending          }           std::cout << "this is going to be 1: " << i << std::endl;}

speaking about your code:

using namespace std; // import everything from std:: into the current scope. including std::count (http://www.cplusplus.com/reference/algorithm/count/) which is now accessible via its name, without namespace prefix int count = 0; // count already exists in the current scope, so creating a new one is ambiguous int main(){          count ++; // which one?}
using namespace std; // import std:: in this outer scope int main(){          int count = 0; // shadowing: count is declared in a different scope (the scope of main())           count ++; // refers to the inner-most declaration of count (the one local to main())}

Thanks for clearing things up. I knew using namespaces imported the whole file, but I didn't know you could have different variables the the same name as long as they're in different scopes. :D

Link to comment
https://linustechtips.com/topic/206395-c-program-help/#findComment-2802220
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×