Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

C++ Output Problem

Options
  • 10-05-2009 12:13pm
    #1
    Closed Accounts Posts: 214 ✭✭


    Hi i'm trying to output a series of strings to one line in a text file, but i'm getting a weird problem where the last three values entered duplicate themselves, ignore the spaces, and become fragmented, code below:

    ReservationInput << Customer.Cust_Name
    << " " << Customer.Cust_Address
    << " " << Customer.Cust_CredNo
    << " " << Customer.CardExp_Date
    << " " << Customer.Card_Company
    << " " << Customer.Card_Status << endl;

    Output:

    JimBob 25NortStreet 12345678912909MA 2909MA MA A


    Any help much appreciated:)


Comments

  • Registered Users Posts: 9,579 ✭✭✭Webmonkey


    did you try couting it to the screen and see if you get similar result


  • Closed Accounts Posts: 214 ✭✭Assmaster_Kronk


    Yep, outputs exactly the same when printed to screen


  • Users Awaiting Email Confirmation Posts: 351 ✭✭ron_darrell


    A cout is a cout - it will only print what you've asked it to. I doubt the problem is with this part of your code. More likely it's with the part that assigns the various values to the various variables. Recheck where the assignment happens. It could be a memory out-of-bounds for the string is causing the other values to be overwritten - a string in c++ is basically a character array. If you tell the array to expect 10 chars but you feed it 16 then it will do as it's told and fill the 10 then borrow the space for the last 6 from someplace else (and usually close by - say the space normally used for the two variables after?)

    -RD


  • Closed Accounts Posts: 214 ✭✭Assmaster_Kronk


    A cout is a cout - it will only print what you've asked it to. I doubt the problem is with this part of your code. More likely it's with the part that assigns the various values to the various variables. Recheck where the assignment happens. It could be a memory out-of-bounds for the string is causing the other values to be overwritten - a string in c++ is basically a character array. If you tell the array to expect 10 chars but you feed it 16 then it will do as it's told and fill the 10 then borrow the space for the last 6 from someplace else (and usually close by - say the space normally used for the two variables after?)

    -RD

    Yeah i double checked the inputs and made the character arrays bigger then they needed to be just to check if anything was overlapping, printing each value separately after the input showed no problems whatsoever, its only when i write it to a file or the screen i get irregularities:confused:


  • Closed Accounts Posts: 214 ✭✭Assmaster_Kronk


    Solved the problem, i just got rid of the structure and had each variable as just normal strings, no idea why it was a problem in the first place though:p


  • Advertisement
  • Registered Users Posts: 26,579 ✭✭✭✭Creamy Goodness


    why is the customer a structure?

    surely it should be a class as you're using C++?

    have all the relevant data as private data memebers of the customer class and write getter functions to access the variables.

    for example.

    customer cust1 = new customer("JimBob", "25NortStreet", "12345678912909MA", "2909MA", "MA", "A");

    /*example print out.*/
    cout << cust1.getName() << endl;

    that being said there's nothing wrong with structures, but i'm only guessing that this is a college assignment for a certain lecturer that i know well and that lecturer will be looking for objects instead of structures.


  • Registered Users Posts: 2,013 ✭✭✭SirLemonhead


    Yeah i double checked the inputs and made the character arrays bigger then they needed to be just to check if anything was overlapping, printing each value separately after the input showed no problems whatsoever, its only when i write it to a file or the screen i get irregularities:confused:

    Why are you not using std::string? :confused:


  • Registered Users Posts: 348 ✭✭SonOfPerdition


    why is the customer a structure?

    surely it should be a class as you're using C++?

    have all the relevant data as private data memebers of the customer class and write getter functions to access the variables.

    for example.

    customer cust1 = new customer("JimBob", "25NortStreet", "12345678912909MA", "2909MA", "MA", "A");

    /*example print out.*/
    cout << cust1.getName() << endl;

    that being said there's nothing wrong with structures, but i'm only guessing that this is a college assignment for a certain lecturer that i know well and that lecturer will be looking for objects instead of structures.

    of course there is nothing wrong with using structures.

    By definition, a struct is a class in which members are by default public; that is, struct s{ is simply shorthand for class s{public

    Bjarne Stroustrup


  • Registered Users Posts: 695 ✭✭✭DaSilva


    Solved the problem, i just got rid of the structure and had each variable as just normal strings, no idea why it was a problem in the first place though:p

    This is just a guess since I can't see the rest of the code, but is it possible some of those strings were not NULL terminated?
    JimBob 25NortStreet 12345678912909MA  2909MA  MA  A
    

    is what you got, and I think you were expecting:
    JimBob 25NortStreet 123456789 12909  M  A
    

    Since Cust_CredNo and the other remaining strings all print all the way to the "A", im guessing none of these strings are NULL terminated except "A", and they are located directly after each other in memory. Therefore cout is just printing from the first character of CredNo until it reaches a NULL which isn't until the Card_Status array.


Advertisement