Small problem in codeing C++ with != (not) function

JonathanM

Senior member
Mar 4, 2002
240
0
0
I get a small error when trying to compile this program... its suppost to do password and user checking for a larger program I'm writing... Any help would be appreciated. (Dang thats what I get for not doing C++ for over a year)

The compiler is VC++ 6.0 Pro SP 5

void main()
{
char Hello;
cout << "Hello World!/n";
cout << "Please say Hello/n";
cin >> Hello;
Hello = toupper(Hello);
while(Hello != "HELLO");
{
cout << "That is not Hello./n";
cout << "Please Try Again./n";
cin >> Hello;
Hello = toupper(Hello);
}
cout << "Thank You";
}

Error Message:

Compiling...
File 1.cpp
C:\Programming Projects\C & C++\Project 1\File 1.cpp(12) : error C2446: '!=' : no conversion from 'char *' to 'int'
This conversion requires a reinterpret_cast, a C-style cast or function-style cast
C:\Programming Projects\C & C++\Project 1\File 1.cpp(12) : error C2040: '!=' : 'int' differs in levels of indirection from 'char [6]'
Error executing cl.exe.

File 1.exe - 2 error(s), 0 warning(s)
 

manly

Lifer
Jan 25, 2000
11,367
2,375
136
The != operator can be used for the primitive numeric data types, and ideally only the integral ones (not for the floating point types).

Applying it to a C string is incorrect. Actually, you declared the variable Hello as a char, so it's not even a string type.

To compare C strings, you can use strcmp, or preferably strncmp.
 

Humanoid

Member
May 6, 2001
104
0
0
ok..here's the solution

#include <iostream.h>
#include <stdlib.h>
#include <string.h> // for strlen

void main()
{
char Hello[5];
int size,loop;
bool match; // to prevent the function from looping back if the condition is met

cout << "Hello World!\n";
match=false;
while (match==false)
{
cout << "Please say Hello\n";
cin >> Hello;
size =strlen(Hello);

for (loop=0;loop<=size;loop++)
{
Hello[loop]=toupper(Hello[loop]);
}

if (strcmp(Hello,"HELLO")!=0)
{
cout << "That is not Hello.\n";
cout << "Please Try Again.\n";
match=false;
}
else
{
cout << "Thank You\n";
match=true;
}
}
}
 

JonathanM

Senior member
Mar 4, 2002
240
0
0
that works... now one more question... is there anyway to do the cin >> Hello, without using a loop or an array?

It will be very hard to expand when you're doing a user/pw check for a much larger program later on
 

alisajid

Member
Jun 29, 2001
194
0
0
Yes use a std::string.

// include up top

#include <string>

// somewhere in your code

std::string foo;
cin >> foo;

That should do the job.
 

PowerYoga

Diamond Member
Nov 6, 2001
4,603
0
0
also on a note, you should match string with string, and numbers with numbers... for example, if you type a letter in the place of an integer, you'll get some weird errors or huge numbers. However, if you type in numbers as strings, they will display correctly. However, when you try to add them, the numbers won't add.

For example, if you declare X as string, and cin "11" and you add it with "12", as opposed to getting 23, you'll get "1112".

there's a command that converts everything to lowercase/uppercase (its toupper i think), so you can use that to do checking with the "hello world" program. Just remember to keep the strings.

 

javathehut

Senior member
Oct 23, 2000
318
0
0
A even cleaner way to do this is:

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

void main()
{
char Hello[5] = {0};

cout << "Hello World!\nPlease say Hello\n";
cin >> Hello;

while (stricmp(Hello,"HELLO")) /* stricmp compares strings case-insensitive */
{
cout << "That is not Hello.\nPlease Try Again.\n";
cin >> Hello;
}

cout << "Thank You\n";
}
 
sale-70-410-exam    | Exam-200-125-pdf    | we-sale-70-410-exam    | hot-sale-70-410-exam    | Latest-exam-700-603-Dumps    | Dumps-98-363-exams-date    | Certs-200-125-date    | Dumps-300-075-exams-date    | hot-sale-book-C8010-726-book    | Hot-Sale-200-310-Exam    | Exam-Description-200-310-dumps?    | hot-sale-book-200-125-book    | Latest-Updated-300-209-Exam    | Dumps-210-260-exams-date    | Download-200-125-Exam-PDF    | Exam-Description-300-101-dumps    | Certs-300-101-date    | Hot-Sale-300-075-Exam    | Latest-exam-200-125-Dumps    | Exam-Description-200-125-dumps    | Latest-Updated-300-075-Exam    | hot-sale-book-210-260-book    | Dumps-200-901-exams-date    | Certs-200-901-date    | Latest-exam-1Z0-062-Dumps    | Hot-Sale-1Z0-062-Exam    | Certs-CSSLP-date    | 100%-Pass-70-383-Exams    | Latest-JN0-360-real-exam-questions    | 100%-Pass-4A0-100-Real-Exam-Questions    | Dumps-300-135-exams-date    | Passed-200-105-Tech-Exams    | Latest-Updated-200-310-Exam    | Download-300-070-Exam-PDF    | Hot-Sale-JN0-360-Exam    | 100%-Pass-JN0-360-Exams    | 100%-Pass-JN0-360-Real-Exam-Questions    | Dumps-JN0-360-exams-date    | Exam-Description-1Z0-876-dumps    | Latest-exam-1Z0-876-Dumps    | Dumps-HPE0-Y53-exams-date    | 2017-Latest-HPE0-Y53-Exam    | 100%-Pass-HPE0-Y53-Real-Exam-Questions    | Pass-4A0-100-Exam    | Latest-4A0-100-Questions    | Dumps-98-365-exams-date    | 2017-Latest-98-365-Exam    | 100%-Pass-VCS-254-Exams    | 2017-Latest-VCS-273-Exam    | Dumps-200-355-exams-date    | 2017-Latest-300-320-Exam    | Pass-300-101-Exam    | 100%-Pass-300-115-Exams    |
http://www.portvapes.co.uk/    | http://www.portvapes.co.uk/    |