• The site migration is complete! Hopefully everything transferred properly from the multiple decades old software we were using before. If you notice any issues please let me know, thanks! Also, I'm still working on things like chatbox, etc so hopefully those will be working in the next week or two.

Need some help with C++

bronzemist02

New member
I am working on a college project for C++ that gets a user to guess a letter. The letters of the alphabet are held in a string from a project file that was asked to be modified for the project.

I set the project file up very similar to that of another guessing game that I found coding for that gets the user to guess a number. I set up my code for this project to create a random number and assign the value to the letter string to get the letter. I know I haven't finished the calling for it yet.

The last few times I went to run the program it came up with over 30 errors most of them to my string comparisons to figure out if the user is correct.

Can anyone help me figure out how to fix these? I think I can figure out the calling afterwards if this is fixed.

These are the errors I have:

1>c:\course technology\83618-4\cpp5\chap12\ch12appe04 solution\ch12appe04 project\ch12appe04.cpp(42) : error C2082: redefinition of formal parameter 'x'
1>c:\course technology\83618-4\cpp5\chap12\ch12appe04 solution\ch12appe04 project\ch12appe04.cpp(43) : error C2679: binary '>>' : no operator found which takes a right-hand operand of type 'overloaded-function' (or there is no acceptable conversion)
1>c:\course technology\83618-4\cpp5\chap12\ch12appe04 solution\ch12appe04 project\ch12appe04.cpp(49) : error C2451: conditional expression of type 'std::basic_string<_Elem,_Traits,_Ax>' is illegal
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>,
1> _Ax=std::allocator<char>
1> ]
1> No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
1>c:\course technology\83618-4\cpp5\chap12\ch12appe04 solution\ch12appe04 project\ch12appe04.cpp(53) : error C2784: 'bool std::operator >(const std::basic_string<_Elem,_Traits,_Alloc> &,const _Elem *)' : could not deduce template argument for 'const _Elem *' from 'int'
1>c:\course technology\83618-4\cpp5\chap12\ch12appe04 solution\ch12appe04 project\ch12appe04.cpp(57) : error C2784: 'bool std::operator <(const std::basic_string<_Elem,_Traits,_Alloc> &,const std::basic_string<_Elem,_Traits,_Alloc> &)' : could not deduce template argument for 'const std::basic_string<_Elem,_Traits,_Alloc> &' from 'int'
1> c:\program files\microsoft visual studio 9.0\vc\include\string(130) : see declaration of 'std::operator <'
1>c:\course technology\83618-4\cpp5\chap12\ch12appe04 solution\ch12appe04 project\ch12appe04.cpp(57) : error C2676: binary '<' : 'std::string' does not define this operator or a conversion to a type acceptable to the predefined operator
==========


This is what I have so far:


//Ch12AppE04.cpp
//Allows the user to guess a letter chosen
//randomly by the computer

#include <iostream>
#include <string>
#include <ctime>
#include <iomanip>
#include <stdlib.h>


using std::cout;
using std::cin;
using std::endl;
using std::string;


using namespace std;

string check (string guess, int x, int letter);

int main()
{
//declare variables
string letters [26] = {"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"};
string guess = "";
int letter = 0;
int x = rand();

//ask the user to guess a number
cout<<"Number: ";
cin>>guess;

cout<< check (guess, x, letter)<<endl<<endl;

return 0;
} //end of main function

int randomNum(int x, string letter)
{
int x = rand()%25;
cin >> letter [x] >> endl;
return x;
}

string check(string guess, int x, int letter){

if(guess=x)
{
cout << "You win, the letter is "<<x<< endl;
}
if(guess>x)
{
cout <<"The number is lower than "<<guess<< endl;
}
if(guess<x)
{
cout <<"The number is lower than "<<guess<< endl;
}




}//end of program
 


1>c:\course technology\83618-4\cpp5\chap12\ch12appe04 solution\ch12appe04 project\ch12appe04.cpp(53) : error C2784: 'bool std::operator >(const std::basic_string<_Elem,_Traits,_Alloc> &,const _Elem *)' : could not deduce template argument for 'const _Elem *' from 'int'

That is telling you that you cannot use the bool operator ">" in your comparison. You need to try comparing the same kind of variable. You are trying to compare an INT to a STRING. Won't work. That operator is for INT to INT.
 
Ok. Thanks. I will try it out with ints and see if it works.


--

Tried it and that narrowed it down to these three errors from 35. lol.

ap12\ch12appe04 solution\ch12appe04 project\ch12appe04.cpp(27) : error C2440: 'initializing' : cannot convert from 'const char [1]' to 'int'
1> There is no context in which this conversion is possible
1>c:\course technology\83618-4\cpp5\chap12\ch12appe04 solution\ch12appe04 project\ch12appe04.cpp(42) : error C2082: redefinition of formal parameter 'x'
1>c:\course technology\83618-4\cpp5\chap12\ch12appe04 solution\ch12appe04 project\ch12appe04.cpp(43) : error C2679: binary '>>' : no operator found which takes a right-hand operand of type 'overloaded-function' (or there is no acceptable conversion)
 
Ok. Here it is. I still have to figure out how to pass the random number to the letter string and get it to get the appropriate letter.

//Allows the user to guess a letter chosen
//randomly by the computer
//Created/revised by Devan Taylor on February 6th, 2010

#include <iostream>
#include <string>
#include <ctime>
#include <iomanip>
#include <stdlib.h>



using std::cout;
using std::cin;
using std::endl;
using std::string;


using namespace std;

string check (int, int, int);
randomNum (int, string, int);

int main()
{
//declare variables
string letters [26] = {"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"};
int guess = 0;
int randomLetter = 0;
int x = rand();

//ask the user to guess a number
cout<<"Number: ";
cin>>guess;

cout<< check (guess, x, randomLetter)<<endl<<endl;

return 0;
} //end of main function

int randomNum(int x, string letter, int randomLetter)
{
x = rand()%25;
cin >> randomLetter [x];
return x;
}

string check(int guess, int x, int letter){

if(guess=x)
{
cout << "You win, the letter is "<<x<< endl;
}
if(guess>x)
{
cout <<"The number is lower than "<<guess<< endl;
}
if(guess<x)
{
cout <<"The number is lower than "<<guess<< endl;
}




}//end of program
 


I now have the program running without errors, but it is displaying 41 as the number each time and it will say you are correct when you enter anything. I am going to change the if(guess=x) and such to if(guess>26||guess=x), if (guess<26||guess<x) and if(guess=26||guess !=x) or equivalent. I am trying to have this done tonight because I work 9-5:30 tomorrow and it is due tomorrow night.
 
I now have the program running without errors, but it is displaying 41 as the number each time and it will say you are correct when you enter anything. I am going to change the if(guess=x) and such to if(guess>26||guess=x), if (guess<26||guess<x) and if(guess=26||guess !=x) or equivalent. I am trying to have this done tonight because I work 9-5:30 tomorrow and it is due tomorrow night.

the '=' is not a comparison operator... Can't believe I missed that.

'==' is the comparison operator.

What you are doing with this piece of code:

Code:
if(guess=x)

is actually setting guess equal to x. So it always returns true.

To compare you use ==. ie:

Code:
if(guess==x)
 
Here, this is all the logic to guess numbers. I was bored. REALLY bored, and decided to re-write parts of this etc.

What exactly is the assignment? I see you have an alphabet.. are they supposed to guess a letter? The logic is the same, but you need to learn how to use strcmp with char[]'s.. It's easy and fast.

your functions are unnecessary in a program this small. If you decide to keep them, they need editing. You don't even use your random function inside of main at all.....

Otherwise this has everything you need. just fill in your blanks.

Code:
/********************
* I'm bored.. so i played around some
* to change this to guessing letters.. it would be easy
* no need for stupid strings just fill a char array how i did
* and use char[]'s instead of ints. Then use strcmp().
* http://www.cplusplus.com/reference/clibrary/cstring/strcmp/
* I'm assuming your assignment was to do it with an alphabet.. Hence me doing the numbers so you can see
* the logic. You can figure out the rest on your own.
* This compiles just fine on the GNU Compiler running on Debian Linux.
* Real programmers use some kind of POSIX system.. :)
*/

#include <iostream>
#include <ctime>
#include <iomanip>
#include <stdlib.h>

using namespace std;

int main()
{
//declare variables

// WHY HAVE AN ALPHABET?? I DONT KNOW!! You had one!
char alphabet[26] = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v$
int guess = 0;                  // the number we are guessing
int randomLetter = 0;           // the randomletter (it's a type int???) I'd rename to just 'random'
int x;                          // why declare this and set as rand()? Waste of time.


// create our random number
srand((unsigned)time(NULL));    // you need use srand before rand.. (look it up! Seeds the generator)
x=rand()%25;                    // keeps us under 25


// while they haven't quit.. or guessed right.... WE MAKE THEM PLAY!
while(1)
{
 //ask the user to guess a number
 cout<<"Guess a number (0 to quit!): ";
 cin >> guess;

 if (guess==0)
  {
        cout << "\n\nThanks for playing!" << endl << endl;
        break;
  }

 cout << "YOUR GUESS WAS: " << guess << endl << endl;

 // debug
// cout << "THE RANDOM NUMBER WAS: " << x << endl << endl;

 // we decide if guess = x.
 if(guess==x)
  {
        cout << "\n\nYou win!!!!!\n\nThe number was " << x << endl;
        break;
  }

 if(guess>x)
  {
        cout <<"The number was lower than " << guess << endl;
  }

 if(guess<x)
  {
        cout <<"The number was higher than " << guess << endl;
  }
} // end of while

} // end of main
 
Last edited:
Thanks a lot. Yes. The project is to get the user to guess a letter. I'll try it and see if I can get it working with strcmp and the char arrays.
 


I tried to get it working with strcmp and char arrays and it wouldn't let me compare the two arrays because the letters one had a value of [26].
 
ok

you compare the guess to alphabet[x]

and out of curiousity are you in northern or lower ontario?
 
Last edited:
I did set it to that at one point. It wouldn't let me compare the other char to the alphabet one though, that is why I got that error. I'm not in Ontario at all. I'm in P.E.I.
 
I am working on another assignment that gets a user to input an employee code in two parts (letter and number) using two variables. It then opens a file and checks it for the characters entered and displays the proper lines. It has no errors, but it only wants to display the last line in the file. Also, my if statement keeps saying that the p# codes are invalid. I have spent at least 6 hours on this so far and can't get it to work. Any help or suggestions would be greatly appreciated.

This is what I have so far:

//Ch13AppE02.cpp
//Displays the names of salespeople having
//a specific code entered by the user; the codes and
//names are stored in a sequential access file
//Created/revised by <your name> on <current date>

#include <iostream>
#include <string>
#include <algorithm>
#include <fstream>

using std::cout;
using std::cin;
using std::endl;
using std::string;
using std::ifstream;
using std::ios;

//variable declaration
void displayCode();


int main()
{
string code= "";
string codenum= "";


//Enter a code to be searched
cout << "Enter an employee code to be searched: "<< endl;
cin>>code;

cout << "Enter an employee number code to be searched: "<< endl;
cin>>codenum;



//call appropriate function
//or display an error message
while (code !="x" && codenum !="x")
{
if (code =="p" && codenum =="1"|| code =="p" && codenum =="2")

displayCode();

if (code =="f" && codenum =="1"|| code =="f" && codenum =="2")
displayCode();
else
cout<<"You have entered an incorrect code. Please enter f1, f2, p1 or p2." << endl;

return 0;

}//end while

}// end of main function


void displayCode()
{
//reads records from a file
//and then displays them
string code= "";
string codenum= "";


//open the file
ifstream inFile;
inFile.open("namecode.txt", ios::in);

//determine if the file is open
if (inFile.is_open())
{
//read a record
inFile >> code>>codenum;


}//end if
while (!inFile.eof())
{
//read another record
getline(inFile, code);
getline(inFile, codenum);
inFile >> code>>codenum;
}//end while
if (inFile.eof()){


inFile.close();
}


//display the code and code number
cout << "The code is: " << code <<codenum<< endl;







}//end of displayCode function
 
Back
Top