I get the following error when I try to compile this code. Can anyone help me make sense of this error?

I have tried defining keystroke as a string and a char, I get the error either way.

1>------ Build started: Project: Register, Configuration: Debug Win32 ------
1>Compiling...
1>Register.cpp
1>i:\c++\register\register\register.cpp(121) : error C2446: '!=' : no conversion from 'const char *' to 'int'
1> There is no context in which this conversion is possible
1>i:\c++\register\register\register.cpp(121) : error C2040: '!=' : 'int' differs in levels of indirection from 'const char [2]'
1>i:\c++\register\register\register.cpp(126) : error C2440: '=' : cannot convert from 'std::string' to 'char'
1> No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
1>Build log was saved at "file://i:\c++\Register\Register\Debug\BuildLog.htm"
1>Register - 3 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

/* Program Name:Register.cpp

   Description: 

   Name:Scott Streit			Class No.: 4467
   Date:11/14/09			  	VisualC++
*/
//********************************** Includes
#include <cfloat>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <cctype>
#include <iomanip>
#include <cmath>
#include <fstream>
#include <string>
#include <ctime>
#include <conio.h>
#include <windows.h>
#include <sstream>

#define cls system("cls")
#define frz system("pause");
#define yl  system("color 0e");

using namespace std;

//********************************** Type definitions
struct cashregister
{
	string key;
	string item;
	double price;
};//end structure

ifstream inputfile ("i:\\c++\\food.txt");
ofstream print ("i:\\c++\\register.txt");
//********************************** Function Prototypes
void initalize(cashregister menu[26], cashregister order[26]);
void checkdatafile();
void getdata(cashregister menu[26]);
void takeorder(cashregister menu[26], cashregister order[26]);
void math();
void displayorder();
void printtofile();
//********************************** Main Function


int main()
{
	time_t t;
	time(&t);
	yl;
	cls;
	cashregister menu[26];
	cashregister order[26];

	//function calls
	initalize(menu, order);
	checkdatafile();
	getdata(menu);
	takeorder(menu, order);
	math();
	displayorder();
	printtofile();


       
	frz;
	return 0;
} // end of main function

//********************************** Function Definitions
void initalize(cashregister menu[26], cashregister order[26])
{
  	int i;
	for(i=0; i<26; i++)
	{
		menu[i].key="";
		menu[i].item="";
		menu[i].price=0.0;
	}
}// end initialize
void checkdatafile()
{
	if(!inputfile)
	{
		cout<<"Error opening data file\n";
		frz;
		exit(1);
	}//end if
}//end checkdatafile
void getdata(cashregister menu[26])
{
	for(int i=0; i<26; i++)
	{
		getline(inputfile, menu[i].key);
		getline(inputfile, menu[i].item);
		inputfile>>menu[i].price;		
		inputfile.ignore(80,'\n');
	} // end for
	inputfile.close();
	
	
	
	//for (int i=0;i<26;i++)
	//{
	//	cout<<menu[i].key<<"\t"<<menu[i].item<<"\t"<<menu[i].price<<"\n";
	//}//end temporary display item
} // getdata
void takeorder(cashregister menu[26], cashregister order[26])
{
	string phrase;
	char keystroke;
	int count=0;
	phrase="Welcome to Streit's Burger Stop";
	cout<<setw((phrase.length()/2)+40)<<phrase<<"\n";
	cout<<"\nPlease begin entering your order.\n";

	while ((keystroke=getch())!="=")
	{
		count++;
		cin>>keystroke;
			for (int i=0;i<26;i++)
				if (keystroke=menu[i].key)
				{
					menu[i].item=order[count].item;
					menu[i].price=order[count].price;
				}//end if
	}//end while

}//end takeorder
void math()
{
	cout<<"at math\n";
}//end math
void displayorder()
{
	cout<<"at displayorder\n";
}//end display order
void printtofile()
{
	cout<<"at printtofile\n";
}//end printtofile

Dani AI

Generated

As observed, the immediate cause is mixing a character literal and a string literal — single quotes produce a char, double quotes produce a string literal — so comparing char to "=" will fail. See the C++ character-literal rules for details. (cppreference.net)
As noted, Visual C++ emits a deprecation warning for the old getch name; use _getch on MSVC. _getch returns an integer and uses a two-call pattern for extended keys (first call yields 0 or 0xE0, second call yields the extended code). (learn.microsoft.com)

Key fixes and patterns (minimal, focused):

  • Use _getch() and store its return in an int (not a std::string).
  • Compare to a character literal ('=') or to the first character of a std::string (menu[i].key[0]) — do not compare a char to a std::string.
  • Use == for comparison (not = which does assignment).
  • When transferring a menu entry into the next order slot, assign into the order (order = menu), not the other way around.

Example pattern (illustrative):

int ch, placed = 0;
while ((ch = _getch()) != '=') {
  for (int i = 0; i < 26; ++i) {
    if (!menu[i].key.empty() && menu[i].key[0] == ch) {
      order[placed].item  = menu[i].item;
      order[placed].price = menu[i].price;
      ++placed;
      break;
    }
  }
}

Troubleshooting notes: conio.h is nonstandard (Windows/DOS-specific); prefer _getch on MSVC or use standard std::cin.get() / platform libraries for portable code. Avoid mixing _getch() with cin >> because one is unbuffered immediate input and the other is formatted buffered input. (en.wikipedia.org)

Recommended Answers

All 3 Replies

while ((keystroke=getch())!="=")

here "=" and '=' are different.
You should use the '='. because "=" is a string literal ( there is a string pool concept that string literals are normally put in the const pool). So
that is the wrong in that line 121

here "=" and '=' are different.
You should use the '='. because "=" is a string literal ( there is a string pool concept that string literals are normally put in the const pool). So
that is the wrong in that line 121

when I define keystroke as char and use single quotes I get this error.

1>------ Build started: Project: Register, Configuration: Debug Win32 ------
1>Compiling...
1>Register.cpp
1>i:\c++\register\register\register.cpp(121) : warning C4996: 'getch': The POSIX name for this item is deprecated. Instead, use the ISO C++ conformant name: _getch. See online help for details.
1> c:\program files\microsoft visual studio 9.0\vc\include\conio.h(145) : see declaration of 'getch'
1>i:\c++\register\register\register.cpp(126) : error C2440: '=' : cannot convert from 'std::string' to 'char'
1> No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
1>Build log was saved at "file://i:\c++\Register\Register\Debug\BuildLog.htm"
1>Register - 1 error(s), 1 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========


However when I define keystroke as string and use double quotes I only get the warning message.

1>i:\c++\register\register\register.cpp(121) : warning C4996: 'getch': The POSIX name for this item is deprecated. Instead, use the ISO C++ conformant name: _getch. See online help for details.
1> c:\program files\microsoft visual studio 9.0\vc\include\conio.h(145) : see declaration of 'getch'

Any thoughts why that is so?

As the error says, the name getch() is deprecated, and is recommended instead.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.