Thread: Lot of questions.

  1. #76
    C++ noob Ryan0773's Avatar
    Join Date
    Jan 2009
    Posts
    43

    Question Thats not right?!?!?!

    Quote Originally Posted by Elysia View Post
    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!!!!
    If you start seeing people in code, it means two things... either you are a genuise with programming or you'll never get a date.

  2. #77
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    937
    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;
    }
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  3. #78
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Interesting solution, but are we going to confuse the newbie?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #79
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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;
    }
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #80
    C++ noob Ryan0773's Avatar
    Join Date
    Jan 2009
    Posts
    43
    Quote Originally Posted by Elysia View Post
    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 View Post
    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
    ----ed up so badly?
    If you start seeing people in code, it means two things... either you are a genuise with programming or you'll never get a date.

  6. #81
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Ryan0773
    You code works, it gives me the average of the number, but im just wondering how my code
    ----ed up so badly?
    Which code are you talking about, and how does it not work?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #82
    C++ noob Ryan0773's Avatar
    Join Date
    Jan 2009
    Posts
    43
    Quote Originally Posted by laserlight View Post
    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 View Post
    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!!!!
    If you start seeing people in code, it means two things... either you are a genuise with programming or you'll never get a date.

  8. #83
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #84
    C++ noob Ryan0773's Avatar
    Join Date
    Jan 2009
    Posts
    43
    Quote Originally Posted by laserlight View Post
    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.
    If you start seeing people in code, it means two things... either you are a genuise with programming or you'll never get a date.

  10. #85
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #86
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    937
    Yes, you must ensure that your IDE is building the correct project.
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  12. #87
    C++ noob Ryan0773's Avatar
    Join Date
    Jan 2009
    Posts
    43
    Quote Originally Posted by laserlight View Post
    How are you compiling and running your test programs?
    Quote Originally Posted by CodeMonkey View Post
    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.
    If you start seeing people in code, it means two things... either you are a genuise with programming or you'll never get a date.

  13. #88
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    937
    Create a new project with only the above code, and tell us what happens.
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  14. #89
    C++ noob Ryan0773's Avatar
    Join Date
    Jan 2009
    Posts
    43
    Quote Originally Posted by CodeMonkey View Post
    Create a new project with only the above code, and tell us what happens.
    ...which "above code", the average one?
    If you start seeing people in code, it means two things... either you are a genuise with programming or you'll never get a date.

  15. #90
    C++ noob Ryan0773's Avatar
    Join Date
    Jan 2009
    Posts
    43
    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.
    If you start seeing people in code, it means two things... either you are a genuise with programming or you'll never get a date.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Several Questions, main one is about protected memory
    By Tron 9000 in forum C Programming
    Replies: 3
    Last Post: 06-02-2005, 07:42 AM
  2. Trivial questions - what to do?
    By Aerie in forum A Brief History of Cprogramming.com
    Replies: 23
    Last Post: 12-26-2004, 09:44 AM
  3. Questions.
    By anonytmouse in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 05-19-2004, 02:16 PM
  4. C++ test questions
    By Mister C in forum C++ Programming
    Replies: 9
    Last Post: 09-08-2002, 12:05 PM

Tags for this Thread