# why isnt this checking my string properly

• 01-17-2007
Mythic Fr0st
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
• 01-17-2007
twomers
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
• 01-17-2007
Salem
> 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.
• 01-17-2007
Mythic Fr0st
ah
Ok, converting integer to string problems...
I can do it if I do
Quote:

std::istringstream i2s("hello");
but not if I do
Quote:

std::istringstream i2s; and then do
getline(cin, i2s);
because getline is asking where i2s is? im confused, I googled this

Quote:

#include <iostream>
#include <sstream>

using namespace std;

int main ()
{
string usr;
string pw;
string em;
getline(cin, usr);
int ul = strlen("Hi");
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<<"Age: "<<age<<"\n";
cout<<"Email: "<<em<<"\n";
}
cout<<"\n";
}

cin.get();
}
• 01-17-2007
Daved
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().
• 01-17-2007
Mythic Fr0st
hmm
I dont have myflush() or mypause()

just

cin.get();
• 01-17-2007
Daved

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.
• 01-17-2007
Mythic Fr0st
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(); }```
• 01-17-2007
Daved
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.
• 01-17-2007
Mythic Fr0st
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

```        std::string line;         std::cin >> line;         std::stringstream ios(line);```