string comparisons

H

Horn

I am having trouble comparing strings. When I run the program and I
type in what I think is the right string, it goes to the else part of
the program. This is for my computer science class in high school so
excuse me if you think it's really easy. If you know what I should do
then please send me a message at (e-mail address removed)

//Britton Horn

#include "stdafx.h"
#include <iostream.h>
#include <stdlib.h>
#ifndef _OOSTRING_H
#define _OOSTRING_H
#include <string.h>

int main()
{
char yourname[25];
char myname[25] = "britton";
int answer;

cout << "What is your name?\n";
cin.get(yourname, 25);

if (yourname == myname)
{
cout << "What is 2 + 2?\n";
cin >> answer;
cout << "Correct!\n";
}
else
{
cout << "What is 2 + 2?\n";
cin >> answer;
if (answer == 4)
{
cout << "I'm sorry. The answer is 22.\n";
}
if (answer != 4)
{
cout << "I'm sorry. The answer is 4.\n";
}
}
return 0;
#endif
}
 
W

WW

Horn said:
I am having trouble comparing strings. When I run the program and I
type in what I think is the right string, it goes to the else part of
the program. This is for my computer science class in high school so
excuse me if you think it's really easy. If you know what I should do
then please send me a message at (e-mail address removed)


Posted AND mailed. LAST TIME! Post here and read here. Answers to my
mailbox will be deleted unread:

http://www.parashift.com/c++-faq-lite/how-to-post.html#faq-5.4
//Britton Horn

#include "stdafx.h"

Non-standard header
#include <iostream.h>

Non-standard header
#include <stdlib.h>
#ifndef _OOSTRING_H
#define _OOSTRING_H

WHY on Earth is this in a header???
#include <string.h>

int main()
{
char yourname[25];
char myname[25] = "britton";
int answer;

cout << "What is your name?\n";
cin.get(yourname, 25);

if (yourname == myname)

Learn about strcmp. Or better: learn about std::string.

http://www.dinkumware.com/manuals/reader.aspx?b=p/&h=string.html#strcmp

http://www.dinkumware.com/manuals/reader.aspx?b=p/&h=string2.html

http://www.google.com/search?hl=en&lr=&ie=UTF-8&oe=UTF-8&safe=off&q=std::string+tutorial+c++

In short: http://tinyurl.com/pm6r
{
cout << "What is 2 + 2?\n";
cin >> answer;
cout << "Correct!\n";
}
else
{
cout << "What is 2 + 2?\n";
cin >> answer;
if (answer == 4)
{
cout << "I'm sorry. The answer is 22.\n";
}
if (answer != 4)
{
cout << "I'm sorry. The answer is 4.\n";
}
}
return 0;
#endif

WHY on Earth is this (the main function) in a header???
 
A

Aggro

Horn wrote:

#include <string.h>

int main()
{
char yourname[25];
char myname[25] = "britton";
int answer;


if (yourname == myname)

Any particular reason why not using the std::string? If you must use
char arrays, you can't compare them like this. You need to use strcmp()
function, or write one by yourself. If you use std::string you can
compare strings like that. Have a look at these two examples:

-------------------------------
#include <string>
#include <iostream>

int main()
{
std::string a = "hello";
std::string b = "hello";

if( a == b )
{
std::cout << "Strings are equal." << std::endl;
}
else
{
std::cout << "Strings are not equal." << std::endl;
}

return 0;
}
--------------------------

------------------------------
#include <cstring>
#include <iostream>

int main()
{
char c[] = "hello";
char d[] = "hello";

if( strcmp( c,d ) == 0 )
{
std::cout << "Strings are equal." << std::endl;
}
else
{
std::cout << "Strings are not equal." << std::endl;
}

return 0;
}
--------------------------------
 
W

WW

Aggro said:
Any particular reason why not using the std::string? If you must use
char arrays, you can't compare them like this. You need to use
strcmp() function, or write one by yourself. If you use std::string
you can compare strings like that. Have a look at these two examples:

Fish, or fishing?
 
K

Kris Wempa

Horn said:
int main()
{
char yourname[25];
char myname[25] = "britton";
int answer;

cout << "What is your name?\n";
cin.get(yourname, 25);

if (yourname == myname)

You can't compare character strings like this. You may be able to do this
for C++ string data types, but not for traditional C character strings. You
need to do this:

if (strcmp(yourname,myname) == 0)
cout << "What is 2 + 2?\n";
cin >> answer;
if (answer == 4)
{
cout << "I'm sorry. The answer is 22.\n";
}
if (answer != 4)
{
cout << "I'm sorry. The answer is 4.\n";
}
}

Is this part working correctly ?
 
K

Kevin Goodsell

Horn said:
I am having trouble comparing strings. When I run the program and I
type in what I think is the right string, it goes to the else part of
the program. This is for my computer science class in high school so
excuse me if you think it's really easy. If you know what I should do
then please send me a message at (e-mail address removed)

I won't, and it's rude to ask. Post here, read here.
//Britton Horn

#include "stdafx.h"

Incomplete code makes it difficult to diagnose problems. You haven't
given us the contents of this header file.
#include <iostream.h>

This is not a standard C++ header. Standard C++ uses said:
#include <stdlib.h>

This header is deprecated in favor of said:
#ifndef _OOSTRING_H
#define _OOSTRING_H

This is illegal. You may not use identifiers beginning with an
underscore followed by either another underscore or an uppercase letter.
They are reserved for the implementation's use. Even if your compiler
fails to diagnose this (most won't), it is an error. A compiler may
reject the code, or may even accept it with a dramatically altered meaning.

The simplest rule of thumb is *never use an identifier that begins with
an underscore, or contains a sequence of two underscores.*

Besides that, "include guards" make no sense in a non-header file.
#include <string.h>

This header is deprecated in favor of <cstring>, but you should just use
<string> and the std::string class instead (as others have already said).
-Kevin
 
A

Ashish

Horn said:
I am having trouble comparing strings. When I run the program and I
type in what I think is the right string, it goes to the else part of
the program. This is for my computer science class in high school so
excuse me if you think it's really easy. If you know what I should do
then please send me a message at (e-mail address removed)

//Britton Horn

#include "stdafx.h"
#include <iostream.h>
#include <stdlib.h>
#ifndef _OOSTRING_H
#define _OOSTRING_H
#include <string.h>

int main()
{
char yourname[25];
char myname[25] = "britton";
int answer;

cout << "What is your name?\n";
cin.get(yourname, 25);

if (yourname == myname)

You cant compare string like this. This will compare the addresses pointed
to by 'yourname' and 'myname'. To compare string, use...

if(strcmp(yourname, myname) == 0)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,731
Messages
2,569,432
Members
44,832
Latest member
GlennSmall

Latest Threads

Top