Welcome to HBH! If you have tried to register and didn't get a verification email, please using the following link to resend the verification email.

Simple C++ Help


reaper4334's Avatar
Member
0 0

Well basically… I need to check if a number is whole or not. It's declared as a float and it's user input. The function I'm making needs to know whether the number's whole or not to determine the next step… I've looked around but I can't find anything… I'm sure it's just a simple function… so does anyknow know it?

Thanks, Reaper


reaper4334's Avatar
Member
0 0

Okay forget the last post. I figured it out:

float i;
cin >> i;
float b = int(i);
if (b < i)
{
cout << "Number wasn't whole!";
}
else
{
cout << "Number was whole!";
}

(that's just a sloppy example to show how it can be done… not my code)

Anyway, I've now come accross another problem. I need to go through a string character by character… I don't know how. I think I'd use a "for" loop, but that's all I've got. I have looked around, but I haven't found anything. Anyone help?

Thanks, Reaper


reaper4334's Avatar
Member
0 0

Haha.. well… yet again I've done it my own strangely figured out way… and I'm stuck somewhere else now.

I have a string that's like: a1.b.c (just an example) and I need to parse a1, b and c seperately. Any ideas?

Thanks (again), Reaper


ynori7's Avatar
Future Emperor of Earth
0 0

use string tokens. you can use a for loop, and check the character at the current index. if that character is a period or whatever token you want to use, set another string variable equal to the substring from 0 to index. then set the original string equal to the substring from index+1 to string.length, then set index back to zero and continue in the for loop. this site would be useful http://www.fredosaurus.com/notes-cpp/strings/header-string.html


reaper4334's Avatar
Member
0 0

I could parse the text.. using the same thing I used to parse the string char by char… I just don't know how many variables I would need. If it's a1.b.c all the time, it could work easily. But it could be a1.b.c.d (these are just syntax examples) … so how would I make a program that makes the amount of variables it needs?

Note: If you said this in your post I appologize, I only read it quickly and it confused me a little.

Thanks, Reaper


ghost's Avatar
0 0

and DONT USE STRTOK()! it returns some silly shit that you cant manipulate as a char array or char* O.o


ynori7's Avatar
Future Emperor of Earth
0 0

if you have an unknown number of variable, use a vector. just keep adding to the end until you're done.


reaper4334's Avatar
Member
0 0

Ummm… how would I go about doing so? (I haven't got a clue what you mean :S)

I'll have a search now… but I thought I'd ask aswel…

Thanks, Reaper


ghost's Avatar
0 0
{
for ( int i =0; i < str.length(); i++ )
if ( str[i] == '.' )
return 0;
else
return 1;
}```

should be like
whole if . isn't found
decimal if it is

ok and your tokenizing problem,something like

```markup//loop
if ( str[i] == '.' )
string s = str.substr(lastPosAfterDot,str[i-1]);```

or use getline with . as the delimiter a set amount of times that there are "."

reaper4334's Avatar
Member
0 0

Well I've figured out how I can do most of the thing I've asked in this thread after receiving tips from people, so thanks.

I am, however, stuck again. This time I'm not looking for something new… but I seem to be doing something wrong… and I don't know what…

The whole code works fine… but then when I add:

       {
           if (text2[i] == ".")
           {
                       end = end + fromnum(p);
           }
           else if (text2[i] == "/")
           {
                p = p + 0.5;
           }
           else
           {
               p++;
           }
           
       }```

to one of my functions... I get quite a lot of errors...
I know it's that bit, 'cause I commented it out just to make sure and the program ran that way.

Anyone see any bugs?
Also, I know I could use switch there... I did originally, but I had the same error and changed to if, else to see if it would change that.

Thanks,
Reaper

ghost's Avatar
0 0

use single quotes around single characters


ynori7's Avatar
Future Emperor of Earth
0 0

is text2 an array or a vector? cuz if it isnt, that'd screw things up, and you have to make sure that it's a string vector too. also, why are you putting it into a for loop if you're only gonna go through one iteration?


reaper4334's Avatar
Member
0 0

The for loop goes through every part of string and text2 is a string. Thankyou, I put single quotes and it worked. Now, my last problem is… I need to parse "2" as 2 … how would I go about doing this?

Thanks, Reaper


reaper4334's Avatar
Member
0 0

Lmao. Yet again, forget my question. I made my own function to convert any number string upto 27 into an integer. Thanks for everyone's help. I've pretty much finished the project now. The only thing I want to do is figure out if I can compile and use it as a dll, I have no idea how that works or anything, but I'll look around. If anyone would like to help me with that, it would be a appreciated.

Thanks everyone, Reaper