why isnt this checking my string properly

This is a discussion on why isnt this checking my string properly within the C++ Programming forums, part of the General Programming Boards category; Code: for (int aoe = 0; aoe <= 9; aoe++) { for (int aue = 1; aue <= 9; aue++) ...

  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,262
    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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,420
    > 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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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 03:21 PM.

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    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 03:41 PM.

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    In this thread: c++ not showing a variable 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,317
    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 04:20 PM.

  11. #11
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,456
    Code:
    	std::string line;
    	std::cin >> line;
    	std::stringstream ios(line);
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

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, 02:40 AM
  4. lvp string...
    By Magma in forum C++ Programming
    Replies: 4
    Last Post: 02-26-2003, 11:03 PM
  5. ........ed off at functions
    By Klinerr1 in forum C++ Programming
    Replies: 8
    Last Post: 07-29-2002, 09:37 PM

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