Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Classes and strcpy()

Status
Not open for further replies.

grande

Programmer
Feb 14, 2005
657
CA
So, I'm working with classes in C++, and I'm not exactly the class-master, so you'll have to bear with me.

The following is my .AddNew method for my Customer class.
Code:
void Customer::AddNew(char n[], char a[], char c[], char p[], float ab)
{
   strcpy(n, name);
   strcpy(a, addy);
   strcpy(c, city);
   strcpy(p, PCode);
   AccBal = ab;
}

Now after putting some cout's and getch()s in there, I've come to the conclusion that the program errors out after changing the name, but before adding the address. I think my address strcpy is the problem. BUT, they're all the same, as far as I can tell.

BTW, the error I get is the standard Windows XP "This program sucks, so send us an error report" dialog box. My compiler is Bloodshed Dev-C++ v4.

Any help would be appreciated.

-------------------------
Just call me Captain Awesome.
 
The code you have will copy whatever string you have in name into n, addy into a... Is that what you intended?
If not, switch the strcpy() parameters around.

Also, are the char* arrays big enough to hold the strings that you are copying?

You might want to switch to STL string objects instead of char* arrays.
 
Okay, I figured it out (it was me being a dummy).

Now, another question, how can I check if a string is storing a number, specifily a float?

-------------------------
Just call me Captain Awesome.
 
If you want to check if that string represents a number, you can use the isdigit() function on each character of the string.
 
You have to be careful with the isdigit technique, since at least one character in a valid float is not a digit (the decimal point).

I would use a stringstream to store the string and then attempt to read the stringstream into a float variable. If it succeeds (the return value converts to true) then the string is a float. Otherwise it is not.
Code:
#include <sstream>
#include <iostream>
#include <string>

bool IsValidFloat(const char* input)
{
    std::istringstream strm(input);
    float test;
    if (!(strm >> test))
        return false;

    // Test if extra stuff left in string:
    std::string leftovers;
    strm >> leftovers;
    return leftovers.empty();
}

int main()
{
    const char* goodFloat = "-123.456";
    const char* badFloat = "I am not a float.";
    const char* notQuiteFloat = "98.76 plus 3";

    if (IsValidFloat(goodFloat))
        std::cout << "goodFloat     is a good float" << std::endl;
    else
        std::cout << "goodFloat     is NOT a good float" << std::endl;

    if (IsValidFloat(badFloat))
        std::cout << "badFloat      is a good float" << std::endl;
    else
        std::cout << "badFloat      is NOT a good float" << std::endl;

    if (IsValidFloat(notQuiteFloat))
        std::cout << "notQuiteFloat is a good float" << std::endl;
    else
        std::cout << "notQuiteFloat is NOT a good float" << std::endl;
}
You could also do something similar with atof.
 
I think using atof() is a bad idea though because if the string isn't a float atof() might crash your program.

I thought about using a stringstream too, but I've never tried outputting to a number, only inputting, so I didn't know if it would work. :)
 
Check out Boost's [tt]lexical_cast[/tt] at for a relatively painless way of performing these kinds of conversions. It uses a [tt]stringstream[/tt] under the covers.
 
cpjust said:
I think using atof() is a bad idea though because if the string isn't a float atof() might crash your program.
I don't think it will crash, but it might return 0.0 if the input is invalid, in which case it is hard to tell whether the string was not a float or just equivalent to 0.0.
 
if atof doesnt find a float in the string it just returns 0. Im sure this is portable behaviour.
 
Maybe, but I know I've had very bad things happen when I pass the wrong parameter type to a function that uses the elipsis operator.
 
atof doesn't use the ellipsis operator, it takes a const char* only. Maybe you are thinking of sprintf? :)
 
In case anyone was wondering, I used an If that was pretty much
if ((IsDigit(MyChar[#]) || (MyChar[#] == '.'))

Thanks for all the help!

-------------------------
Just call me Captain Awesome.
 
Sorry, I'm a total dumbass when I've had 4 hours of sleep! ;) You're right, for some reason I was thinking of sprintf().
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top