Simple C++ Help
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
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
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
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
{
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 "."
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
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