# Lot of questions.

Show 80 post(s) from this thread on one page
Page 6 of 7 First 1234567 Last
• 01-09-2009
Ryan0773
Thats not right?!?!?!
Quote:

Originally Posted by Elysia
Solution 1:
Code:

```bool flag = true; while (!flag) {     switch (...)     {         // ...         default:             flag = false;     } }```
Solution 2:
Code:

```while (...) {     switch (...)     {         // ...         default:             continue;     } }```
It is not at all that hard as you make it out to be. Learn how to use flowcharts and pseudo code.

I tried them both out but they didn't seem to work, i may be doing something wrong but that doesn't really matter right now, ill learn it later I'm just friggin confused with this. When i input this code it's supposed to find the average of the integers or something.

Code:

```#include <cstdarg> #include <iostream> using namespace std; double average ( int num, ... ) {   va_list arguments;                      double sum = 0;   va_start ( arguments, num );            for ( int x = 0; x < num; x++ )            sum += va_arg ( arguments, double );   va_end ( arguments );                  return sum / num;                      } int main() {   cout<< average ( 3, 12.2, 22.3, 4.5 ) <<endl;   cout<< average ( 5, 3.3, 2.2, 1.1, 5.5, 3.3 ) <<endl;   cin.get(); }```
It's not the best code in the world but im still learning. Anyways, a while back i made a code that multiplied two seperate integers that were input (aren't i amazing), this is relavent i guess because whenever i try to run this code, it keeps popping up my multiplication code. I mean WTF!!!!:confused:
• 01-10-2009
CodeMonkey
Got me thinking....
Code:

```#include <vector> #include <iostream> template<typename T> class average { public:         average(T in, T null_in = 0) : null(null_in)                 { nums.push_back(in); }         const T null;         average & operator()(T n)                 { nums.push_back(n); return *this; }         long double value()         {                 T sum = null;                 for(std::size_t i = 0; i < nums.size(); ++i)                         sum += nums[i];                 return (long double)sum / (long double)nums.size();         } private:         std::vector<T> nums; }; template<typename T> average<T> avg(T first) {         return average<T>(first); } template<typename T> std::ostream & operator << (std::ostream & s, average<T> & a) {         s << a.value();         return s; } int main(int argc, char* argv[]) {         std::cout << avg(3)(22.3345)(76)(2) << std::endl; //bad: 22.3345 will be added as 22         std::cout << avg(22.3345)(3)(76)(2) << std::endl; //good: this is what you want.         //Incidentally, you could just implement it as a double, without the templates.         return 0; }```
• 01-10-2009
Elysia
Interesting solution, but are we going to confuse the newbie? ;)
• 01-10-2009
laserlight
Frankly, it seems much easier to write:
Code:

```#include <numeric> #include <cstddef> #include <iostream> int main() {     double numbers[] = {5, 3.3, 2.2, 1.1, 5.5, 3.3};     const std::size_t size = sizeof(numbers) / sizeof(numbers[0]);     double average = std::accumulate(numbers, numbers + size, 0.0) / size;     std::cout << average << std::endl; }```
• 01-10-2009
Ryan0773
Quote:

Originally Posted by Elysia
Interesting solution, but are we going to confuse the newbie? ;)

...Ya i have no idea what you guys are saying so that might meen that im a bit confused

Quote:

Originally Posted by laserlight
Frankly, it seems much easier to write:
Code:

```#include <numeric> #include <cstddef> #include <iostream> int main() {     double numbers[] = {5, 3.3, 2.2, 1.1, 5.5, 3.3};     const std::size_t size = sizeof(numbers) / sizeof(numbers[0]);     double average = std::accumulate(numbers, numbers + size, 0.0) / size;     std::cout << average << std::endl; }```

You code works, it gives me the average of the number, but im just wondering how my code
• 01-10-2009
laserlight
Quote:

Originally Posted by Ryan0773
You code works, it gives me the average of the number, but im just wondering how my code

Which code are you talking about, and how does it not work?
• 01-10-2009
Ryan0773
Quote:

Originally Posted by laserlight
Which code are you talking about, and how does it not work?

This is the code that i am having trouble with. it says the problem at the end of the quote.

Quote:

Originally Posted by Ryan0773
Code:

```#include <cstdarg> #include <iostream> using namespace std; double average ( int num, ... ) {   va_list arguments;                      double sum = 0;   va_start ( arguments, num );            for ( int x = 0; x < num; x++ )            sum += va_arg ( arguments, double );   va_end ( arguments );                  return sum / num;                      } int main() {   cout<< average ( 3, 12.2, 22.3, 4.5 ) <<endl;   cout<< average ( 5, 3.3, 2.2, 1.1, 5.5, 3.3 ) <<endl;   cin.get(); }```
It's not the best code in the world but im still learning. Anyways, a while back i made a code that multiplied two seperate integers that were input (aren't i amazing), this is relavent i guess because whenever i try to run this code, it keeps popping up my multiplication code. I mean WTF!!!!:confused:

• 01-10-2009
laserlight
Quote:

Originally Posted by Ryan0773
This is the code that i am having trouble with. it says the problem at the end of the quote.

What do you mean by "it keeps popping up my multiplication code"? The program looks okay, compiles okay, and gives the correct results when run.
• 01-10-2009
Ryan0773
Quote:

Originally Posted by laserlight
What do you mean by "it keeps popping up my multiplication code"? The program looks okay, compiles okay, and gives the correct results when run.

Well whenever i run it, instead of running this code, it runs my multiplication code:

Code:

```#include <iostream> using namespace std; int mult (int x, int y); int main () {     int x;     int y;         cout<<"Please input two numbers to be multiplied, after inserting the first number, press enter. Then input the second number and press enter: ";     cin>> x >> y;     cin.ignore ();     cout<<"The product of you two numbers is " << x * y <<"\n";     cin.get (); } int mult ( int x, int y ) {     return x * y; }```
That is the code it runs instead of the other one. I dont know why it keeps running this one instead.
• 01-10-2009
laserlight
Quote:

Originally Posted by Ryan0773
Well whenever i run it, instead of running this code, it runs my multiplication code:

How are you compiling and running your test programs?
• 01-10-2009
CodeMonkey
Yes, you must ensure that your IDE is building the correct project.
• 01-10-2009
Ryan0773
Quote:

Originally Posted by laserlight
How are you compiling and running your test programs?

Quote:

Originally Posted by CodeMonkey
Yes, you must ensure that your IDE is building the correct project.

umm...Like everyone else? I think? I write the code and then press the "Compile & Run" button. Is there another way to compile it? This hasn't ever happenned before with my other projects.
• 01-10-2009
CodeMonkey
Create a new project with only the above code, and tell us what happens.
• 01-10-2009
Ryan0773
Quote:

Originally Posted by CodeMonkey
Create a new project with only the above code, and tell us what happens.

...which "above code", the average one?
• 01-10-2009
Ryan0773
Alright, nevermind, it seems to be working. I'm still wondering how it read the other program. I did everything the same but this time it gave me the averages (which are 13 and 3.08, by the way)
I have a question about learning C++. Would it be a better idea to get a book on the subject (if so, please suggest some) or should i just keep doing the C++ tutorials on the site, but they dont explain things all that well.
Show 80 post(s) from this thread on one page
Page 6 of 7 First 1234567 Last