Thread: Have a better method of doing this?

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    156

    Have a better method of doing this?

    Code:
    #include <iostream>
    #include <fstream>
    
    inline int some(int *a_number, int yournumber)
    {
     a_number= new int[5];
     a_number[0]= yournumber;
    
     return a_number[0];
    }
    
    int main(int argc, char *argv[])
    {
     int number;
     int extra;
     cout << "Input a number: ";
      cin >> number;
    
     extra= some(&extra, number);
     cout << "Your number is: " << extra << endl;
    
     cin.get();
     return 0;
    }
    I'm too depress to explain.
    Compiler: MingW(IDE: Bloodshed Dev-C++ 4.01)
    Web Site: Zoo Crew
    Forums: Zoo Boards
    E-mail: [email protected]

    "Do you wanna go to jail or do you wanna go home?!?!" - Alonzo(Training Day)

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    > I'm too depress to explain.

    Well you'd better explain, because no one knows what the hell you're trying to do.

    You can't just pass a single variable over (or rather, it's address) and treat it as an array of variables.

    Oh, sure, you CAN, but this is what I like to call a BadThing(TM).

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    156
    The Some function saves the user's keyboard input into a buffer(*a_number or &extra in main()). Its pretty simple really.
    Compiler: MingW(IDE: Bloodshed Dev-C++ 4.01)
    Web Site: Zoo Crew
    Forums: Zoo Boards
    E-mail: [email protected]

    "Do you wanna go to jail or do you wanna go home?!?!" - Alonzo(Training Day)

  4. #4
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    It leaks memory, and doesn't input multiple values (no need for buffer).

    Code:
    #include <iostream>
    #include <vector>
    
    int main() {
    	std::vector<int> storage;
    	int current;
    
    	std::cout << "Enter sequence of numbers, enter a letter to quit " << std::endl;
    
    	while (std::cin >> current) {
    		storage.push_back(current);
    	}
    
    	std::cout << "Outputting with array notation"<< std::endl;
    	for (size_t i = 0; i < storage.size(); i++) {
    		std::cout << storage[i] << ' ';
    	}
    
    	std::cout << "\nOutputting with iterator notation" << std::endl;
    	for (std::vector<int>::iterator it = storage.begin(); it != storage.end(); it++) {
    		std::cout << *it << ' ';
    	}
    
    	return 0;
    }
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  5. #5
    Registered User
    Join Date
    Jan 2002
    Posts
    559
    You need a using statement, like using namespace std;, or using std::cout;
    Otherwise you have to do it SilentStrike's way of prefixing std:: to all your cout and cin and other library related statements. Maybe there's a reason to code that way, but I've never heard of it.
    Truth is a malleable commodity - Dick Cheney

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. on method pointers and inheritance
    By BrownB in forum C++ Programming
    Replies: 2
    Last Post: 03-02-2009, 07:50 PM
  2. stuck on display method
    By shintaro in forum C++ Programming
    Replies: 2
    Last Post: 02-01-2009, 05:17 PM
  3. Best communication method to thousand childs?
    By Ironic in forum C Programming
    Replies: 8
    Last Post: 11-08-2008, 12:30 AM
  4. C# method
    By siten0308 in forum C# Programming
    Replies: 6
    Last Post: 07-15-2008, 07:01 AM
  5. Replies: 2
    Last Post: 01-22-2008, 04:22 PM