Struct help!

This is a discussion on Struct help! within the C++ Programming forums, part of the General Programming Boards category; Hey everyone- I am having trouble with structs...grrrr. I am trying to ignore the - sign in the beginning of ...

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    85

    Struct help!

    Hey everyone- I am having trouble with structs...grrrr. I am trying to ignore the - sign in the beginning of the number so I can compute the average. I cant seem to remember how to just ignore the sign or whatever, without using cin.ignore since the number is already read in. Also my average function isnt working right even with the positive numbers. Basically structs confuse the hell out of me! Please give advice if possible! Thanks!

    Overview:
    so I am reading in from a file long integers. I am reading them into a string function and then breaking the numbers up and storing them in an int array. I can convert from string to int and store the numbers ok, I just need to ignore the - sign when storing them in the array, so I can use the numbers to calculate average.

    Ex:
    Code:
     -123456789
    
    average needs to be (1+2+3+4+5+6+7+8+9) / 9
    Thanks!



    Code:
    struct bigint
    {
      string nstr;                                  //string representation of the big integer n
      int n[MAX];                                  //array representation of the n
      int len;                                        //# of digits in n
      bool neg;                                    //flag to indicate sign of n, true if negative, false otherwise
    };
    
    int main()
    {
      int n;
      bigint number[50];                            //number can store the string of a big integer, the array of
                                                               //numbers, the length of the numbers, and the sign of each number
      ifstream infile;                                   //infile represents the input data file
      string ifilename;                                //name for input filenames
      int count=0;                                     //counter for # of numbers in input file
      n = 0;
      int s;
      int average;
    
    
      cout << "Please enter input file name" << endl;
      cin >> ifilename;
    
      infile.open(ifilename.c_str());
    
     infile >> number[n].nstr;
      while (!infile.eof())
        {
          number[n].len = number[n].nstr.length();
          n++;
          infile >> number[n].nstr;
        }
    
      cout << endl << "The " << n << " bigints read were" << endl;
    
      for (int i = 0; i < n; i++)
        {
          cout << number[i].nstr << endl;
        }
    
      cout << endl;
    
      for (int s=0; s < n; s++)
        {
          for (int j=0; j < number[s].len; j++)
            {
              number[s].n[j] = number[s].nstr[j] - '0';
              cout << number[s].n[j];
              average = average + number[s].n[j];
            }
          cout << endl;
          average = average / number[s].len;
          cout << average;
        }
    
      return 0;
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Once you've read the number in as a string, if nstr[0] is '-', set the negative flag, otherwise don't.

  3. #3
    Registered User
    Join Date
    Nov 2006
    Posts
    85
    well i started doing that, but what happens after the flag gets set to true. Does the neg sign get ignored after that?

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Who knows? That depends on your specification. You can't store a negative sign in your int array n, so presumably you would need to "ignore" it when building that array.

  5. #5
    Registered User
    Join Date
    Nov 2006
    Posts
    85
    ok and thats my problem. I only know how to do cin.ignore, but the number has already been read in, so how would I do ignore? you cant just do ignore() right?

  6. #6
    Registered User
    Join Date
    Nov 2006
    Posts
    85
    I got my average function working, but I still cant do anything with the neg. sign. I tried having a string variable hold the neg. sign, but then it prints 0's in front of all my numbers. Please help!


    Code:
    for (int s=0; s < n; s++)
        {
          for (int j=0; j < number[s].len; j++)
            {
              if (number[s].n[0] == '-')
                {
               number[n].neg = true;
               hold = number[s].n[0];
                }
              number[s].n[j] = number[s].nstr[j] - '0';
              average = average + number[s].n[j];
            }
          cout << endl;
          //cout << average/ number[s].len << " is the average of the digits in " << number[s].nstr;
          //average = 0.0;
          for (int l = 0; l < number[s].len; l++)
                 cout << number[s].n[l];
       }

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You don't want to ignore it, because you need it in your string representation of the number. If the symbol is -, do nothing, otherwise subtract '0'.

  8. #8
    Registered User
    Join Date
    Nov 2006
    Posts
    85
    well if i do nothing then it will still read it in and put it in spot 1. I know i need it for future tasks in this assignment, but I do not want it to be stored in the array, at all. That is where I am having a problem. I need to ignore it when storing it in the array, but then be able to use it later which I can do. So for the time being:


    Code:
     if the first spot is a neg. sign
        bypass first spot and continue storing the numbers
     else start storing the numbers

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    So since the pseudocode is exactly right, what's the difficulty here? I realize it's hard to sit there and type absolutely nothing, but if you feel you have to type something do a comment or something, like so:
    Code:
    if (number.nstr[i] == '-') {
       /*TYPE ABSOLUTELY NOTHING HERE */
    } else {
        number.n[j++] = number.nstr[i];
    }
    That's not so hard, right? If you can't manage it, change the condition from == to != so that you can just leave the else off, rather than type nothing in the if part.

  10. #10
    Registered User
    Join Date
    Nov 2006
    Posts
    85
    Tabstop you always help me!

    however, what i have in the pseudocode is what i have in the program but it is STILL storing the stupid neg sign! I dont know whats going on. Maybe my for loop is what is messing it up. I have tried to start j at 1 instead of 0, but then it prints a 0 in the front of my number.

    Code:
     for (int s=0; s < n; s++)
        {
          for (int j=0; j < number[s].len; j++)
            {
              if (number[s].n[0] == '-')
                {
                  number[n].neg = true;
                }
             else
              number[s].n[j] = number[s].nstr[j] - '0';
              ave = ave + number[s].n[j];
            }
          cout << endl;
    This code right here still prints -123456. If i start j at 1, then it prints 0123456, and does that for all the numbers.

    Thanks and sorry
    Last edited by ammochck21; 12-04-2008 at 03:56 PM.

  11. #11
    Registered User
    Join Date
    Nov 2008
    Posts
    41
    Code:
    for (int s=0; s < n; s++)
        {
          int j;
          if (number[s].n[0] == '-')
                {
                  number[n].neg = true;
                  j = 1;
                } else {
                  j = 0;
                }
          for (/*nothing*/; j < number[s].len; j++)
            {
              
             else
              number[s].n[j] = number[s].nstr[j] - '0';
              ave = ave + number[s].n[j];
            }
          cout << endl;
    Try this. Declare j before the inner loop and then set it to either 0 or 1 (if it has a negative sign). This way, it will always store the negative sign but you can just skip over it in the array.

  12. #12
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by ammochck21 View Post
    Tabstop you always help me!

    however, what i have in the pseudocode is what i have in the program but it is STILL storing the stupid neg sign! I dont know whats going on. Maybe my for loop is what is messing it up. I have tried to start j at 1 instead of 0, but then it prints a 0 in the front of my number.

    Code:
     for (int s=0; s < n; s++)
        {
          for (int j=0; j < number[s].len; j++)
            {
              if (number[s].n[0] == '-')
                {
                  number[n].neg = true;
                }
             else
              number[s].n[j] = number[s].nstr[j] - '0';
              ave = ave + number[s].n[j];
            }
          cout << endl;
    This code right here still prints -123456. If i start j at 1, then it prints 0123456, and does that for all the numbers.

    Thanks and sorry
    You get the 0 because you start j at 1. The point you need to realize, and haven't yet, is that the index into the string nstr, and the index into the array of ints n, must not be the same -- things are going into different places, you need different indices!

    Code:
     for (int s=0; s < n; s++)
        {
          for (int j=0; j < number[s].len; j++)
            {
              int arrayindex = 0;
              if (number[s].n[0] == '-')
                {
                  number[n].neg = true;
                }
             else
              number[s].n[arrayindex++] = number[s].nstr[j] - '0';
              ave = ave + number[s].n[j]; /*Not sure what this is doing here, but would need to change */
            }
          cout << endl;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 12-03-2008, 03:10 AM
  2. Global Variables
    By Taka in forum C Programming
    Replies: 34
    Last Post: 11-02-2007, 04:25 AM
  3. Replies: 10
    Last Post: 05-19-2006, 12:23 AM
  4. What's wrong with my search program?
    By sherwi in forum C Programming
    Replies: 5
    Last Post: 04-28-2006, 10:57 AM
  5. Tutorial review
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 03-22-2004, 09:40 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21