# 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. ## 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. Once you've read the number in as a string, if nstr[0] is '-', set the negative flag, otherwise don't.

3. well i started doing that, but what happens after the flag gets set to true. Does the neg sign get ignored after that?

4. 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. 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. 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. 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. 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. 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. 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

11. 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. Originally Posted by ammochck21
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;```