error

closed account (4jzvC542)
I am trying to compile this program but it is showing errors
I am using Microsoft Visual Studio 2006 standard edition.
This is my source code:


-------------------------------------------------
#include <iostream>
#include <conio.h>

using namespace std;

void main()
{
int ch;
int chcnt = 0;
int wdcnt = 1;

while ((ch = getche()) != "\r")
{
if (ch == " ")
wdcnt++;
else
chcnt++;
}
std::cout << "No of Character Entered : " << chcnt << "\n";
std::cout << "No of Words Entered : " << wdcnt << "\n";
}
----------------------------------------------------------------


On compiling it shows this error :

Compiling...
03 conio_words.cpp
F:\00 C++Study 13\03 Home-Work Exercise\chapter10 Functions\examples\03 conio words\03 conio_words.cpp(11) : error C2446: '!=' : no conversion from 'char *' to 'int'
This conversion requires a reinterpret_cast, a C-style cast or function-style cast
F:\00 C++Study 13\03 Home-Work Exercise\chapter10 Functions\examples\03 conio words\03 conio_words.cpp(11) : error C2040: '!=' : 'int' differs in levels of indirection from 'char [2]'
F:\00 C++Study 13\03 Home-Work Exercise\chapter10 Functions\examples\03 conio words\03 conio_words.cpp(13) : error C2446: '==' : no conversion from 'char *' to 'int'
This conversion requires a reinterpret_cast, a C-style cast or function-style cast
F:\00 C++Study 13\03 Home-Work Exercise\chapter10 Functions\examples\03 conio words\03 conio_words.cpp(13) : error C2040: '==' : 'int' differs in levels of indirection from 'char [2]'
Error executing cl.exe.

03 conio_words.obj - 4 error(s), 0 warning(s)

---------------------------------------------------------

Please help me what should do get it right

Parjanya
Firstly ch should be a char and not an integer...
You should consider replacing the double quotation mark with a single one (ie: instead of "\r" use '\r' -- line 11 and instead of " " use ' ' -- line 13).
However, I also recommend moving the assignment from line 11 ch = getche() to, let's say, line 10.

Hope it helps.


Kind regards,
Raul Butuc.
Last edited on
i think getch() or getche() are returning nothing(these are void functions)
No, they are int functions, not void functions.
They return the keycode read.
@AbstractionAnon
you're right my friend !
I must test then talk !
Excuse me !
closed account (4jzvC542)
@jumper007 thanks very much..... it helped

i didn't made this code
it was given in my textbook and i was unable to compile
thank you very much

parjanya
Topic archived. No new replies allowed.