Please help: C++ question

dirtdirt KC
edited December 2004 in Internet & Media
First, I appoligize for asking a question about homework but, when you need help you always go to someone who can.

If an int starts with zeros, how can you force them to be displayed in a cout statement. Right now I'm just getting random numbers displayed if it starts with zeros.

Thank You.

Comments

  • leishi85leishi85 Grand Rapids, MI Icrontian
    edited December 2004
    i 'm not quiet sure what u are asking.

    int i = 0;
    cout << i << endl;

    and it will just display 0
  • dirtdirt KC
    edited December 2004
    I'm reading in 7 digit ints from a .dat file and then trying to display them but when the number that I read in starts with a zero or zeros it displays a random number.
  • leishi85leishi85 Grand Rapids, MI Icrontian
    edited December 2004
    did u read in the file as a vector of ints??

    if u did, then just do

    cout << vector << endl;

    this cout statement will just display that vector.
  • Straight_ManStraight_Man Geeky, in my own way Naples, FL Icrontian
    edited December 2004
    Best way with int processing is not to have lead zeros. So, strip leading zeros and display the rest. IF you are working with binary or hex input, and want decimal out, the base conversion algorithm should strip to 10 of you have a hex 00A and not give 0010 decimal-- result shoudl be stripped to 10 with a lead zero removal applied to output string. IF what you are being fed is in fact floating point, you have to convert to int first, that should leave you with an integer which has not leadign zeros also. Note, the RAND function will produce a random number, if there is a typo in the routine with RAND instead of the proper function call for rounding to take floating point and convert to interger, you will gt RANDOM numeric output indeed. Most languages I have used use RND for round or autoround as part of conversion of numeric base type, and RAND for a random number gen. Look for typos that tell C++ to do the wrong thing, because that is the one commonest way to get what you are getting. Ignore any caps, I used them to show a function call and not to say that function calls need to be capitalized.
  • dirtdirt KC
    edited December 2004
    I am using a while loop to find a number in the .dat file that matches the one given by the user. It then reads in the remaining info from that line of the .dat file. It does calculations on the other info and then displays the user given info and the calculated info.
  • dirtdirt KC
    edited December 2004
    Hey thanks for your help. Problem solved.

    fin >> dec;
    fin int;

    cout.fill (0);
    cout >> setw(7) >> int
    cout.fill (' ')
    :D
Sign In or Register to comment.