-
IF statement
I have a problem with the IF statement can somebody help ?
// C++
#include <iostream.h>
#include <string.h>
#include <stdlib.h>
void main()
{
char buffer[10];
char b;
cout << "\n\nType a word: " << endl;
cout << "\n:";
cin.getline(buffer, 10);
if(buffer == "hello")
cout << "The typed word is the same as the embeded word in this program.\n\n";
else
cout << "The typed word is not the same as the embeded word in this program.\n\n";
cout << "\nTyped word: " << buffer << endl;
cout << "\n\nEnter to Exit.\n";
cin.get(b);
}
:confused:
-
>void main()
int main()
>if(buffer == "hallo")
This isn't doing what you think it is. To compare C-style strings you need to use strcmp:
Code:
if ( strcmp ( buffer, "hallo" ) == 0 )
-
Code:
char b;
...
cin.get(b);
that's just a waste of a byte... you can just use:at the end of the program to do the same thing
-
Dear friends,
Thanks for helping me I'm a Newbie but I'm learning verry well
The Example code is working
greetz Cyberbjorn
-
Dear people,
I want to give this Example A color background and the char's
how can I make it so, here is the code:
// C++
#include <iostream.h>
#include <string.h>
#include <stdlib.h>
void main()
{
char buffer[10];
char b;
cout << "\n\nType a word: " << endl;
cout << "\n:";
cin.getline(buffer, 10);
if ( strcmp ( buffer, "hallo" ) == 0 )
cout << "The typed word is the same as the embeded word in this program.\n\n";
else
cout << "The typed word is not the same as the embeded word in this program.\n\n";
cout << "\nTyped word: " << buffer << endl;
cout << "\n\nEnter to Exit.\n";
cin.get(b);
}
friendly greetz Cyberbjorn
-
>I want to give this Example A color background and the char's
Read this.
>void main()
While you're at it, read this too.
-
Just wanted to get you to know you should use the standard template library.
#include <iostream.h>
#include <string.h>
#include <stdlib.h>
instead use these
#include <iostream>
#include <cstring>
#include <cstdlib>
Good luck.