which variable can store words?

This is a discussion on which variable can store words? within the C++ Programming forums, part of the General Programming Boards category; i'm new and just now learning from the totorial. i am trying to make a program where you are given ...

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    4

    which variable can store words?

    i'm new and just now learning from the totorial. i am trying to make a program where you are given a spanish word and you must translate it to english. (this is just me messing around for learning purposes) I'm using an if statement and if they enter the right word for the variable, the program says you're correct or wrong. It works when I use an "int" variable but i don't know how to make a word into a variable. would I use the "char" variable?

    (in the following code i use an int variable so it works)
    Code:
    #include <iostream>
    using namespace std;
    int main()
    {
           int dormir;
          
           cout<<"welome to my spanish program!\n";
           cout<<"Translate each spanish word into english. Here is the first question out of ten.\n";
           cout<<"Dormir\n";
           cin>> dormir;
           cin.ignore();
           if (dormir == 2) { cout<<"correct!\n"; }
           else if (dormir != 2) { cout<<"incorrect...\n"; }
           system("Pause");
    }

  2. #2
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    #include <string>

    string

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    4
    did you mean to use string instead of int or char or float? i tried that, but it didn't work. sorry if i'm seeming too oblivious with this stuff...

  4. #4
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,475
    Code:
    #include <string>
    #include <iostream>
    
    int main()
    {
       std::string dormir;
    
       std:cout << "Enter a number: ";
       std::cin >> dormir;
    
       if ( dormir == "2" )
    }
    Is how your program would roughly work if you used strings. But since you ask for integral value, and integer would be better.
    I'm just trying to be a better person - My Name Is Earl

  5. #5
    Code Injector Gaming's Avatar
    Join Date
    Mar 2008
    Posts
    19
    To get a string it would be getline(cin, yourstringnamehere);
    Do #include <string> first
    and declare a string: string randomstring
    strings are just a collection of characters, except they can support spaces, so they can be very long, depending on how you do it. so string[500] would be loooooooooong.

  6. #6
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    Quote Originally Posted by Gaming View Post
    To get a string it would be getline(cin, yourstringnamehere);
    Do #include <string> first
    and declare a string: string randomstring
    strings are just a collection of characters, except they can support spaces, so they can be very long, depending on how you do it. so string[500] would be loooooooooong.
    huh?

    each individual string can be as big as you want it to be, as long as you have the memory for it. so string[500] would only create an array of 500 different strings.

    just try it.
    Code:
    #include <iostream>
    #include <string>
    
    int main()
    {
        std::string test;
        std::cout << "Creating string... It will take a long time if it even finishes.\n";
        for ( size_t x = 0; x != test.npos-1; ++x )
            test += "1";
        std::cout << "String size: " << test.length() << std::endl;
        system("pause");
        return 0;
    }
    That just keeps adding a '1' to the string. Until it reaches the highest possible size for a string.

    There is a perfect example. That program locks up my system. Maxes my pagefile out to 4GB. Also uses all of my 2 gig of RAM.

    edit: Solid state hard-drives are the best. Almost as fast access as memory.

  7. #7
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,435
    String is an object in C++ to handle strings automatically for you.
    The above would hold true for "char" in C because it's basically an array you need to make a C-style string.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    A sequence of characters is called a string.

    In C++, there two types (or styles) of strings (just to make things interesting). The cprogramming.com tutorial covers C-Style strings. These are simply an array of characters, terminated by a null (zero value). ...The null-termination means you can create an array that holds 1000 characters, but the program will know that the actual string stops at the null. And, to make things even more interesting, these things have more than one name. Sometimes they are called "C-style strings", "character arrays", or "null-terminated strings".

    There is a set of functions in the <cstring> header for manipulating and comparing C-style strings. With C-style strings, it's easy to understand what's going-on "under the hood", and you can easily see that it takes more than one memory-address to store a string. (It's not as obvious with C++ string objects, but still true.)

    C++ String objects, used with the <string> header, are generally easier to use, but most tutorials and books cover C-Style strings first. (This may be because it's harder to understand what's going-on "under the hood", especially since you usually learn strings before you learn about classes & objects.)

    i'm new and just now learning from the totorial.
    Tutorials are a good start, but I strongly recommend a good beginning C++ book. A book will cover essentially the same information, but with a whole chapter (or more) of explanation and detail on each topic.

  9. #9
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    Quote Originally Posted by Hunterofman View Post
    i'm new and just now learning from the totorial
    Try this:

    http://www.mindview.net/Books/TICPP/...ngInCPP2e.html

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting a grasp on pointers
    By lilrayray in forum C Programming
    Replies: 23
    Last Post: 07-26-2006, 06:58 PM
  2. Begginer Problem: Extracting words from sentence
    By barlas in forum C++ Programming
    Replies: 5
    Last Post: 05-04-2006, 03:17 PM
  3. Question about printing a variable???
    By Hoser83 in forum C++ Programming
    Replies: 2
    Last Post: 03-31-2006, 12:57 PM
  4. Printing out 2 largest words
    By 0rion in forum C Programming
    Replies: 2
    Last Post: 06-02-2004, 12:51 AM
  5. store date in a variable
    By actionbasti in forum C++ Programming
    Replies: 2
    Last Post: 10-01-2003, 09:50 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21