# Find average using a vector keeps displaying nan

• (2 Pages)
• 1
• 2

## 24 Replies - 1213 Views - Last Post: 12 January 2019 - 02:39 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=414251&amp;s=f2b73a193d4cd31c35448e436292418c&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #16 albert003

Reputation: 22
• Posts: 614
• Joined: 15-December 14

## Re: Find average using a vector keeps displaying nan

Posted 09 January 2019 - 11:05 PM

OOoops you're right. Can't believe I missed that parameter. Thanks again.

### #17 albert003

Reputation: 22
• Posts: 614
• Joined: 15-December 14

## Re: Find average using a vector keeps displaying nan

Posted 10 January 2019 - 10:27 AM

Ooooops you're right I missed that and it was right under my nose. Ok, I corrected that mistake. Thank you for pointing that out for me.

This is the program.

```#include <iostream>
#include <string>
#include <vector>

{
double input;
int number;
std::cout << "How many grades will you enter?.\n" <<std::endl;
std::cin >> number;

for(int i = 0; i < number; i++)
{
std::cout << i+1 << " grade" <<std::endl;
std::cin >> input;
homework.push_back(input);
}
return homework;
}
std::string getName()
{
std::string name;
std::cout << "Enter your full name:" << std::endl;
std::getline(std::cin,name);

return name;
}
{
double average;
double total = 0;
for(int i = 0; i < homework.size();i++)
{
total += homework[i];
}

average = total / homework.size();

return average;
}
void showResults(const std::string &name,double average)
{
std::cout << name << "'s average grade is:  "<< average << std::endl;
}

int main()
{
double average;
std::string name = getName();
average = getAverage(homework);
showResults(name,average);
}

```

### #18 ndc85430

• I think you'll find it's "Dr"

Reputation: 934
• Posts: 3,741
• Joined: 13-June 14

## Re: Find average using a vector keeps displaying nan

Posted 10 January 2019 - 11:47 PM

Good. Now the functions look like they're each doing one thing only and their signatures make sense. In future, strive for design like this.

The only very minor complaint I have is about the splitting of the declaration and initialisation of the variable average in main (lines 50 and 53, respectively). Why not do both things in one line like the others?

### #19 albert003

Reputation: 22
• Posts: 614
• Joined: 15-December 14

## Re: Find average using a vector keeps displaying nan

Posted 10 January 2019 - 11:57 PM

Honestly, I didn't know you could do it in one line. I always thought you had to declare the variable in one line and then use in in another line. Similar to what I did in line 32 and 39. I'll make that change and try to remember that for future programs.

### #20 ndc85430

• I think you'll find it's "Dr"

Reputation: 934
• Posts: 3,741
• Joined: 13-June 14

## Re: Find average using a vector keeps displaying nan

Posted 11 January 2019 - 01:40 AM

albert003, on 11 January 2019 - 06:57 AM, said:

Honestly, I didn't know you could do it in one line.

This doesn't make any sense. Look at line 51, for example. You're declaring and initialising on one line there, aren't you?

### #21 jimblumberg

Reputation: 5652
• Posts: 17,353
• Joined: 25-December 09

## Re: Find average using a vector keeps displaying nan

Posted 11 January 2019 - 10:24 AM

Quote

I always thought you had to declare the variable in one line and then use in in another line. Similar to what I did in line 32 and 39.

This also doesn't make sense since it should also be declared and initialized in the same place. You really should study up on the

Quote

RAII
concept. In C++ variables should be defined as close to first use as possible and whenever possible initialize the variable at the same time.

```double getAverage(const grades& homework)
{
//    double average; // Try to define variables closer to first use!
double total = 0;
for(int i = 0; i < homework.size();i++)
{
total += homework[i];
}

//  double average = total / homework.size(); // If you must use a variable initialize it close to first use!

//    return average; // But in this case you don't even need this variable, just return the "average"!

}
```

But in this case, as pointed out in an earlier post, you don't even need that variable, just return the desired value.

By the way you should be careful of just taking code and pasting it into your programs without closely studying the copied code. If you would have closely studied the code you copied from an earlier post you should have realized that it possible to initialize your variables at the time of their definition since that copied code illustrated that concept.

For example your original code in main()
```    grades homework;
std::string name;
double number,input,avg;
int x = 0;
std::cout << "What is the students first and last name:" <<std::endl;
std::getline(std::cin,name);
enter(homework,number,input,x);
average(name,homework);
}
```

And the code you appear to have copied from one of the posts:

```    std::string name = getName();
```

By the way since you seem to have come into some money recently, I suggest you consider buying a more current book instead of relying on the semi outdated free book you're currently using. But almost any book is better that the approach you had been taking.

Quote

I'm a visual person and understand better when I have examples in front of me.

By the way there is no such thing as a "visual person". You really need to try to overcome this limitation and learn research the written material you don't understand. In this age of information you are only a few clicks away from many different explanations of the issues you don't understand. But by all means, when dealing with programming, strive to stay away from videos as much as possible. IMO, programming howto videos are usually extremely poor choices when trying to learn a programming topic. They usually skip over important minor details and just as often are produced by programming novices with limited knowledge and rarely if ever actually tell you the whole story.

Jim

### #22 albert003

Reputation: 22
• Posts: 614
• Joined: 15-December 14

## Re: Find average using a vector keeps displaying nan

Posted 12 January 2019 - 10:05 AM

ndc85430 You're right I did do that previously. I'll make sure to initiate it at the same time I'm using it in my program.

jimblumberg I actually found the free PDF download for both books. I've been using that to study accelerated c++. You're right and I will work on that short coming.

### #23 jimblumberg

Reputation: 5652
• Posts: 17,353
• Joined: 25-December 09

## Re: Find average using a vector keeps displaying nan

Posted 12 January 2019 - 10:12 AM

Quote

Well I gave you those books as alternatives to more modern works because you said you were a poor student. But since you have now come into some money you should really consider buying a more modern book to learn modern C++ (C++14 or C++17) instead of C++98 with a little C++11, if you can afford a new laptop then surely you can afford a modern C++ book if you really want to learn C++.

Jim

### #24 albert003

Reputation: 22
• Posts: 614
• Joined: 15-December 14

## Re: Find average using a vector keeps displaying nan

Posted 12 January 2019 - 01:08 PM

Will do. I'll look on amazon and look for more recent books. Is there a particular author you recommend?.

### #25 jimblumberg

Reputation: 5652
• Posts: 17,353
• Joined: 25-December 09

## Re: Find average using a vector keeps displaying nan

Posted 12 January 2019 - 02:39 PM

Quote

Is there a particular author you recommend?

Not off the top of my head.