Help with outputting a list of squared numbers

This is a discussion on Help with outputting a list of squared numbers within the C++ Programming forums, part of the General Programming Boards category; So my program is suppose to take in a single number and output all numbers starting from 0 up to ...

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    110

    Help with outputting a list of squared numbers

    So my program is suppose to take in a single number and output all numbers starting from 0 up to that given number squared. I believe this is quite an easy problem but I am struggling with it when it includes headers and source files. I am getting an 'undefined reference to 'squaredint...'' error. I do not understand why it is getting this error though, but I do see where it occurs.
    Here is the coding for my main, source, and header files.

    main
    Code:
    #include "squaredintheader.h"
    #include <conio.h>
    #include <iostream>
    
    using std::cout;
    using std::endl;
    using std::cin;
    
    int main(){
        
        int num;
        
        cout << "Up to what number do you want squared? " << endl;
        cin >> num;
        
        //the whole squared numbers
        number_info numbers = squaredint(num, numbers);
        
        //outputs the number with the squared numbers
        for(int i = 0; i != numbers.number.size(); i++){
                cout << i << " " << numbers.squared[i] << endl;
                }
                
        cout << "Press enter to continue..";
        _getch();
        return 0;
        }
    header
    Code:
    #ifndef GUARD_squarednum_h
    #define GUARD_squarednum_h
    
    #include <vector>
    
    //squaredintheader.h
    struct number_info{
           std::vector<int> number;
           std::vector<int> squared;
    };
    
    number_info squaredint(const int&, number_info&);
    
    #endif
    source
    Code:
    #include "squartedintheader.h"
    #include <vector>
    
    using std::vector;
    
    number_info squaredint(const int& upto, number_info& squaredlist){
               
                //adds the given number and squared number
                //to the struct of int vectors
                for(int i = 0; int != upto; i++){
                        squaredlist.number.push_back(i);
                        squaredlist.squared.push_back(i*i);
                        }
                }
    Thanks for your help!
    Last edited by dnguyen1022; 01-15-2009 at 02:57 PM.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You have to compile all the source files. (See other thread today about same topic.)

  3. #3
    Registered User
    Join Date
    Nov 2008
    Posts
    110
    okay, so I fixed a few problems I noticed with my code and tried to compile the source file. It is giving me an 'undefined reference to 'WinMain@16' error.

    source code
    Code:
    #include "squaredintheader.h"
    
    #include <vector>
    
    using std::vector;
    
    number_info squaredint(const int& upto, number_info& squaredlist){
                //adds the given number and squared number
                //to the struct of int vectors
                for(int i = 0; i != upto; i++){
                        squaredlist.number.push_back(i);
                        squaredlist.squared.push_back(i*i);
                        }
                }
    Could this be a compiler problem?

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    No, I'm pretty sure it's a you problem. You need to compile all your source files, not just some of them.

  5. #5
    Registered User
    Join Date
    Nov 2008
    Posts
    110
    So right...I got it working after putting the files in a single project. However my code doesn't work. It asks me for the number, as soon as I enter one, Windows tells me there is an error and I am forced to close it. I wish I could say where I think I may be wrong, but I don't see anything. Can someone help point out my mistake(s)?

    main
    Code:
    #include "squaredintheader.h"
    #include <conio.h>
    #include <iostream>
    
    using std::cout;
    using std::endl;
    using std::cin;
    
    int main(){
        
        int num;
        
        cout << "Up to what number do you want squared? "; 
        cin >> num;
        
        //the whole squared numbers
        number_info numbers = squaredint(num, numbers);
        
        //outputs the number with the squared numbers
        for(int i = 0; i != numbers.number.size(); i++){
                cout << i << " " << numbers.squared[i] << endl;
                }
                
        cout << "Press enter to continue..";
        _getch();
        return 0;
        }
    header
    Code:
    #ifndef GUARD_squarednum_h
    #define GUARD_squarednum_h
    
    #include <vector>
    
    //squaredintheader.h
    struct number_info{
           std::vector<int> number;
           std::vector<int> squared;
    };
    
    number_info squaredint(const int&, number_info&);
    
    #endif
    source
    Code:
    #include "squaredintheader.h"
    
    #include <vector>
    
    using std::vector;
    
    number_info squaredint(const int& upto, number_info& squaredlist){
                //adds the given number and squared number
                //to the struct of int vectors
                for(int i = 0; i != upto; i++){
                        squaredlist.number.push_back(i);
                        squaredlist.squared.push_back(i*i);
                        }
                }

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    There are several things in your code that aren't that great:
    Code:
    number_info squaredint(const int&, number_info&);
    No point in passing integers as const reference - it just adds overhead.

    Second, you are passing in numbers by reference, and then also returning a number_info struct - that doesn't make sense. Use either a return value or a reference parameter- not both.

    Code:
    struct number_info{
           std::vector<int> number;
           std::vector<int> squared;
    };
    Would it not be better to have a struct of numebr and squared, and make that into a vector?

    Code:
    #include "squaredintheader.h"
    
    #include <vector>
    
    using std::vector;
    I'm pretty sure all of the red parts can be removed - certainly you already have included <vector>, since you do that in squaredintheader.h.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Registered User
    Join Date
    Nov 2008
    Posts
    110
    Ok, thank you for your help, but I am unsure what you meant by it adds "overhead", and second of all...

    Code:
    #include "squaredintheader.h"
    
    number_info squaredint(const int& upto, number_info& squaredlist){
                //adds the given number and squared number
                //to the struct of int vectors
                for(int i = 0; i != upto; i++){
                        squaredlist.number.push_back(i);
                        squaredlist.squared.push_back(i*i);
                        }
                return squaredlist;
                }
    Is that what you meant by a return value? Sorry I am pretty new to programming/c++ terms..as I don't know what you meant by reference either..

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    In C++, a variable/parameter that uses & is a reference (which basically means that it is a pointer, but without the pointer notation for accessing it).

    So, you are passing squaredlist to your function, modifying it, and then copying the same thing out of there when returning the values - I don't know if copying the data to the same object is causing a problem or not, but it certainly doesn't make sense to pass in a struct, modify it, and ALSO copy it as a return value at the same time. I prefer (for large objects like this) to not copy them unnecessarily, so I'd say passing it in by reference is fine, and just make the function a void function - do not return anything.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    Registered User
    Join Date
    Nov 2008
    Posts
    110
    Ohhh, got it!..I think

    Code:
    #include "squaredintheader.h"
    
    void squaredint(const int& upto, number_info& squaredlist){
              
                //adds the given number and squared number
                //to the struct of int vectors
                for(int i = 0; i != upto; i++){
                        squaredlist.number.push_back(i);
                        squaredlist.squared.push_back(i*i);
                        }
                }
    However my program still will not run. It only gets as far as asking for a number and then crashes/errors.

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Yes. Now remove the & in const int & upto as well, and you are good to go. Remember to modify the prototype of the function as well.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  11. #11
    Registered User
    Join Date
    Nov 2008
    Posts
    110
    alright so I tried that and its giving me a 'undefined reference to 'squaredint(int&, number_info&)'. I don't even have the & anymore anywhere with the int. I am using bloodshev C++ by the way..

    Code:
    #include "squaredintheader.h"
    #include <conio.h>
    #include <iostream>
    
    using std::cout;
    using std::endl;
    using std::cin;
    
    int main(){
        
        int num;
        
        cout << "Up to what number do you want squared? "; 
        cin >> num;
        
        //the whole squared numbers
        number_info numbers;
        squaredint(num, numbers);
        
        //outputs the number with the squared numbers
        for(int i = 0; i != numbers.number.size(); i++){
                cout << i << " " << numbers.squared[i] << endl;
                }
                
        cout << "Press enter to continue..";
        _getch();
        return 0;
        }
    header
    Code:
    #ifndef GUARD_squarednum_h
    #define GUARD_squarednum_h
    
    #include <vector>
    
    //squaredintheader.h
    struct number_info{
           std::vector<int> number;
           std::vector<int> squared;
    };
    
    void squaredint(int, number_info&);
    
    #endif
    source
    Code:
    #include "squaredintheader.h"
    
    void squaredint(const int upto, number_info& squaredlist){
                //adds the given number and squared number
                //to the struct of int vectors
                for(int i = 0; i != upto; i++){
                        squaredlist.number.push_back(i);
                        squaredlist.squared.push_back(i*i);
                        }
                }
    Last edited by dnguyen1022; 01-16-2009 at 08:48 AM.

  12. #12
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Did you actually compile both the source files? If you didn't compile the file that contains main, then I would expect that result.

    Edit: Take that back, your problem is the missmatch of "const" and not "const" in your declaration and definition of:
    Code:
    void squaredint(int, number_info&);
    Since you CAN have const and non-const versions of the same function, the compiler won't try to link with the const version when your prototype isn't const.


    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  13. #13
    Registered User
    Join Date
    Nov 2008
    Posts
    110
    Ok, I know what I did wrong. I'm use to using Visual C++ where it naturally rebuilds everything. I just had to rebuild the coding and then compile and run. It works great now. Thanks!

  14. #14
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Code:
    cout << i << " " << numbers.squared[i] << endl;
    If you do this, is there really any point in storing all the i's in a different vector? What purpose does numbers.numbers serve, if you only use it to look up the size? Especially considering numbers.numbers.size() should equal numbers.squared.size()
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  15. #15
    Registered User
    Join Date
    Nov 2008
    Posts
    110
    You bring a good point for efficiency problems, and useless coding. I followed your heed and fixed it! Now its just a struct with a single vector field. But then I also changed that. Now my code is much shorter. Thank you!

    Code:
    #include "squaredintheader.h"
    #include <conio.h>
    #include <iostream>
    #include <iomanip>
    #include <string>
    #include <vector>
    
    using std::cout;
    using std::endl;
    using std::cin;
    using std::setw;
    using std::vector;
    
    int main(){
        
        int num;
        
        cout << "Up to what number do you want squared? "; 
        cin >> num;
        
        //the whole squared numbers
        vector<int> numbers;
        squaredint(num, numbers);
        
        //outputs the number with the squared numbers
        for(int i = 0; i != numbers.size(); i++){
                cout << setw(2) << i  << " " << numbers[i] << endl;
                }
                
        cout << "Press enter to continue..";
        _getch();
        return 0;
        }
    Code:
    #ifndef GUARD_squarednum_h
    #define GUARD_squarednum_h
    
    #include <vector>
    
    //squaredintheader.h
    
    void squaredint(int, std::vector<int>&);
    
    #endif
    Code:
    #include "squaredintheader.h"
    
    using std::vector; 
    
    void squaredint(const int upto, vector<int>& squaredlist){
                //adds the given number and squared number
                //to the struct of int vectors
                for(int i = 0; i != upto; i++){
                        squaredlist.push_back(i*i);
                        }
                }
    By the way is there a function that determines the length of a number as in its width of digits. I searched on google, I don't think im using the right keyterms. .length() and .size() does not seem to work, as it works only for strings? I want my columns to even up in the program.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 26
    Last Post: 07-05-2010, 10:43 AM
  2. Outputting a list of squared Doubles
    By dnguyen1022 in forum C++ Programming
    Replies: 13
    Last Post: 01-19-2009, 12:24 PM
  3. Recursion Revisited again, and again!
    By clegs in forum C++ Programming
    Replies: 93
    Last Post: 12-08-2007, 07:02 PM
  4. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM
  5. List class
    By SilasP in forum C++ Programming
    Replies: 0
    Last Post: 02-10-2002, 04:20 PM

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