Thread: why isnt this checking my string properly

  1. #1
    Registered User
    Join Date
    Jan 2007
    Posts
    43

    why isnt this checking my string properly

    Code:
        for (int aoe = 0; aoe <= 9; aoe++)
        {
            for (int aue = 1; aue <= 9; aue++)
            {
                if (allowedage[aoe] == age)
                {
                    age_correct = true;
                }
                else if (age == "0")
                {
                     age_correct = true;
                }
                else if (aoe > 0)
                {
                    if (allowedage[aoe]+allowedage[aue] == age)
                    {
                        age_correct = true;
                    }
                }
            }
        }
    allowedage is a string from 0 to 9

    some reason this isnt working

    it should make it able to check the age up to 99 and as low as 0

    Im using a string because, my .exe file shutsdown if you enter a string, and I dont want that AND getline(cin, age); doesn't work if its an Integer

    anyway, what it should do is...

    check if 1,2 3, 4, 5, 6, 7, 8, 9, is equal to age.. (age being the value entered by the person)
    then, if not, check if 0 is equal to age, if not, check if
    10, 11, 12, 13, all the way to 99, is equal to age

    all I get is "23 is not a valid age" or a number I put in

  2. #2
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    Is it a std::string, or a char array? What type is age? Why don't you have the for loop variable as a char?

    Code:
    for  ( char whatever='0'; whatever<'0'; whatever++ ) { [...] }
    might make it easier

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > Im using a string because, my .exe file shutsdown if you enter a string,
    So let's begin with getting the input side working properly, rather than trying to repair it later on in this function with some hackery.

    Post the first bit of your code.

    > getline(cin, age); doesn't work if its an Integer
    You read a line into a std::string, using getline
    You then convert that string (using one of several methods) to an integer.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Jan 2007
    Posts
    43

    ah

    Ok, converting integer to string problems...
    I can do it if I do
    std::istringstream i2s("hello");
    but not if I do
    std::istringstream i2s; and then do
    getline(cin, i2s);
    because getline is asking where i2s is? im confused, I googled this



    #include <iostream>
    #include <sstream>

    using namespace std;

    int main ()
    {
    string usr;
    string pw;
    string em;
    cout<<"Please signup below,"<<"\n"<<"\n";
    cout<<"Username: ";
    getline(cin, usr);
    int ul = strlen("Hi");
    cout<<"Password: ";
    getline(cin, pw);
    int pl = strlen("OMG");
    cout<<"Age: ";

    std::istringstream i2s;

    getline(cin, i2s);
    int age;
    i2s >> age;
    std::cout << "Value is " << age << '\n';
    cout<<"E-mail: ";
    getline(cin, em);

    for (int rt = 0; rt <= 25; rt++)
    {
    if (rt == 9)
    {
    cout<<"Welcome to Mythic Aeons, "<<"\n"<<"\n"<<"Details: "<<"\n";
    cout<<"Username: "<<usr<<"\n";
    cout<<"Password: "<<pw<<"\n";
    cout<<"Age: "<<age<<"\n";
    cout<<"Email: "<<em<<"\n";
    }
    cout<<"\n";
    }


    cin.get();
    }
    Last edited by Mythic Fr0st; 01-17-2007 at 04:21 PM.

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Just use cin >> to read in the age. If your exe shuts down immediately when you do that, it is because cin >> leaves a newline in the input stream. Simply call your my_flush() function from the other thread before calling my_pause().

  6. #6
    Registered User
    Join Date
    Jan 2007
    Posts
    43

    hmm

    I dont have myflush() or mypause()

    just

    cin.get();
    Last edited by Mythic Fr0st; 01-17-2007 at 04:41 PM.

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    In this thread: http://cboard.cprogramming.com/showthread.php?t=87539 you had those functions. They do exactly what you need.

    If you don't want to use those functions, then simply putting cin.get() twice will often work as well because get() does something similar to the code in myflush.

    Reading everything into a string and then converting it is always an option, but IMO it just complicates the issue, especially when your original problem is a simple one that happens all the time and has a much easier fix.

  8. #8
    Registered User
    Join Date
    Jan 2007
    Posts
    43

    erm

    But I cant use getline(cin, int) it has to be a string...

    it wont compile otherwise

    I also am trying to convert the string "user" to lowercase, with no luck...

    Code:
    #include <iostream>
    #include <sstream>
       	//string::size_type len = user.length();
       	//strlen("hi");
    
    using namespace std;
    
    int main ()
    {
        string user; // (the string user)
        string pw;
        string em;
        int age;
        cout<<"Please signup below,"<<"\n"<<"\n";
        cout<<"Username: ";
        getline(cin, user); // (writes the info the person types)
        user = tolower(user); // converts to lowercase, (DOESNT WORK!!))
        
        cout<<"Password: ";
        getline(cin, pw);
        cout<<"Age: ";
        cout<<"E-mail: ";
        getline(cin, em);
        
           for (int rt = 0; rt <= 25; rt++)
           {
               if (rt == 9)
               {
                  cout<<"Welcome to Mythic Aeons, "<<"\n"<<"\n"<<"Details: "<<"\n";
                  cout<<"Username: "<<user<<"\n";
                  cout<<"Password: "<<pw<<"\n";
                  cout<<"Age: "<<age<<"\n";
                  cout<<"Email: "<<em<<"\n";
               }
               cout<<"\n";
           }
           
        
        cin.get();
    }

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    I'm suggesting you use cin >> age to read in the age. After you read in the age, there will be a newline left in the stream that you have to flush. Calling cin.get() will do that. Your myflush() function from the other thread will also do that.

    Such a simple change will allow you to read in the age as an integer without getline.

    For the lower case thing, you cannot use tolower on a string. You use it on a single character. If you have a string, you could use a loop to lower each character in it.

  10. #10
    Registered User
    Join Date
    Jan 2007
    Posts
    43

    hmm

    I tried

    I found a thing, but I couldnt do

    user = user + c

    (c being the char, and user the string)

    to add it, it kept being null, so I deleted it

    thanks for your help (alot:P)
    Last edited by Mythic Fr0st; 01-17-2007 at 05:20 PM.

  11. #11
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Code:
    	std::string line;
    	std::cin >> line;
    	std::stringstream ios(line);
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. char Handling, probably typical newbie stuff
    By Neolyth in forum C Programming
    Replies: 16
    Last Post: 06-21-2009, 04:05 AM
  2. Replies: 4
    Last Post: 04-03-2007, 05:57 AM
  3. Custom String class gives problem with another prog.
    By I BLcK I in forum C++ Programming
    Replies: 1
    Last Post: 12-18-2006, 03:40 AM
  4. lvp string...
    By Magma in forum C++ Programming
    Replies: 4
    Last Post: 02-27-2003, 12:03 AM
  5. ........ed off at functions
    By Klinerr1 in forum C++ Programming
    Replies: 8
    Last Post: 07-29-2002, 09:37 PM