LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-11-2004, 11:24 PM   #1
PennyroyalFrog
Member
 
Registered: Mar 2004
Location: Michigan
Distribution: Gentoo 2006.1
Posts: 107

Rep: Reputation: 15
C++ character variables


I'm just started to learn C++ from a tutorial on a website while I wait for my two C++ books that i'm getting for the holidays.
My question is: how do you use character variables in an "if" statement in which if a word is inputed that is the same as a certain word in the if statement, it continues with the rest of if statement to output what you want to be said if the two words are the same. To clarify..Here's an example of how i thought it would work:


char name [20];
cout << "What is your name? ";
cin >> name;
if (name == "brad") {
cout << "Hi, Brad";
}
else {
cout << "sorry, I do not know you.";
 
Old 12-11-2004, 11:51 PM   #2
Dodgeram01
Member
 
Registered: Jun 2003
Distribution: Gentoo and Ubuntu
Posts: 95

Rep: Reputation: 15
The following code works for me:
Code:
#include <iostream>
#include <string>

using namespace std;

int main(int argc, char *argv[])
{
    string name = "";
    cout << "What is your name? ";
    cin >> name;
    if (name == "brad")
    {
        cout << "Hi, Brad";
    }
    else
    {
        cout << "Sorry, I do not know you.";

        return EXIT_SUCCESS;
    }
}
 
Old 12-12-2004, 12:16 AM   #3
PennyroyalFrog
Member
 
Registered: Mar 2004
Location: Michigan
Distribution: Gentoo 2006.1
Posts: 107

Original Poster
Rep: Reputation: 15
Thanks for the quick reply. What do the following mean?


#include <string>

using namespace std;

int main(int argc, char *argv[])
{
string name = "";
 
Old 12-12-2004, 12:34 AM   #4
Dodgeram01
Member
 
Registered: Jun 2003
Distribution: Gentoo and Ubuntu
Posts: 95

Rep: Reputation: 15
Code:
#include <string>
imports the string.h header file, which allows us to use the new variable type, string.

Code:
using namespace std;
tells the program that we are using the std namespace, so instead of typing std::cout and std::cin, etc, we can simply just type cout and cin, etc.

You can still just use int main(). The one I posted is the fancy smancy one which is used by default in KDevelop.

Code:
string name = "";
Declares the variable name to be of the data type string, which we can now use because we imported the string header file. The rest of the line is initializing the variable to an empty string.
 
Old 12-12-2004, 02:07 AM   #5
PennyroyalFrog
Member
 
Registered: Mar 2004
Location: Michigan
Distribution: Gentoo 2006.1
Posts: 107

Original Poster
Rep: Reputation: 15
thanks i appreciate your assistance.
 
Old 12-12-2004, 04:13 PM   #6
PennyroyalFrog
Member
 
Registered: Mar 2004
Location: Michigan
Distribution: Gentoo 2006.1
Posts: 107

Original Poster
Rep: Reputation: 15
a couple more questions:

what if i wanted to repeat a char variable that has a space in it
Code:
char name [30];
cout << "What's your full name? ";
cin >> name; //input would be something like Brad Smith
cout << name << ", that's a nice name";
when i try to use spaces the second word just goes to the next cin << variable;

also,
in simpler C++ programs i've made, i didn't have to use the "using namespace std;" for using cout and cin but it seems in the one with the string header file that i do have to use it. How come?
 
Old 12-12-2004, 08:55 PM   #7
Dodgeram01
Member
 
Registered: Jun 2003
Distribution: Gentoo and Ubuntu
Posts: 95

Rep: Reputation: 15
Code:
#include <iostream>

using namespace std;

int main()
{
    char name [30];
    cout << "What's your full name? ";
    cin.getline(name, 30); //input would be something like Brad Smith
    cout << name << ", that's a nice name";

    return EXIT_SUCCESS;
}
The new part here is the use of the getline method of cin. The variable is passed in as the first parameter, and the size is passed in as the second parameter.

As far as your second question goes, regarding
Code:
using namespace std;
I can't tell you why for some of your simpler programs didn't require it, but I would tend to guess they just assumed it since it is so common.
 
Old 12-12-2004, 09:08 PM   #8
Dodgeram01
Member
 
Registered: Jun 2003
Distribution: Gentoo and Ubuntu
Posts: 95

Rep: Reputation: 15
And, for your learning pleasure, a combination of both of your questions, overly commented so that it will hopefully be easy for anyone to understand:
Code:
#include <iostream>
#include <string>

using namespace std;

int main()
{
    char name [30];     // Declare variable which will allow us to take input
                        // from user easily

    string name2 = "";  // Declare a string variable which will allow us to
                        // easily compare the value

    cout << "What's your full name? ";

    cin.getline(name, 30);  // Take the current line, up to 30 characters,
                            // and assign it to the character array name

    name2 = name;           // Set the value of the string name2 to that of the
                            // characer array name

    cout << name2 << ", that's a nice name.\n";

    if (name2 == "Brad Smith")  // Make the comparison of string name2 to
    {                           // "Brad Smith"
        cout << "Hey, I think I know you!\n";
    }
    else
    {
        cout << "I don't think I've met you before.\n";
    }


    return EXIT_SUCCESS;
}
And for comparison purposes, the same code (uncommented) written in Python:
Code:
name = raw_input("What is your full name? ")
print name+", that's a nice name."
if name == "Brad Smith":
    print "Hey, I think I know you!"
else:
    print "I don't think I've met you before."

Last edited by Dodgeram01; 12-12-2004 at 09:13 PM.
 
Old 12-12-2004, 10:25 PM   #9
PennyroyalFrog
Member
 
Registered: Mar 2004
Location: Michigan
Distribution: Gentoo 2006.1
Posts: 107

Original Poster
Rep: Reputation: 15
Thanks for being such a big help. I was experimenting with cin.getline in my program and on the second one it asks, it seems to skip the input and procedes to output the next 'cout'. i'd rather not post all the code but i'm guessing something in there is screwing it up.
 
Old 12-13-2004, 03:38 AM   #10
james.farrow
Member
 
Registered: Mar 2003
Location: UK Darlington
Distribution: Fedora Freebsd Centos
Posts: 296

Rep: Reputation: 31
As fas as I know, cin and cout is buffered. That is, cin reads until the first space is 'read' and takes it as the input. The 'rest' of what was typed is still in the buffer. If there is another cin what is in the buffer is 'read' staright in. So it looks as if the prog 'skipped' the cin and went to the cout.
You can use cin.ignore(). This ignores the next char in the buffer.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
C character appending Chrax Programming 6 12-23-2004 11:32 AM
Character replacement SeT Linux - General 1 11-18-2004 12:21 PM
Insert character into a line with sed? & variables in sed? jago25_98 Programming 5 03-11-2004 06:12 AM
Shel scripting: variables pointing to variables and case Dark_Helmet Programming 5 06-08-2003 11:07 AM
^M character david_wliu Linux - General 6 03-16-2003 11:16 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:30 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration