avaris Posted March 21, 2007 Share Posted March 21, 2007 Hey dudes I've been lookin through the internet and two of my books and there are two things I can't figure out in c++(visual). 1. Subroutines. Before I would just do return main(); and it would redirect me to the beggining of the program. Since I am writing more involved code I actually need to set up subroutines to have direct control over where the code is being redirected. I just need to know the actual syntax/code. 2. I have to use an Excel file for input and output in a quicksort. Would I do #include then the file directory on my computer for the file? How would get and write information from the file? Again just the syntax/code is all I need. Thanks! Quote Link to comment Share on other sites More sharing options...
Kanthos Posted March 21, 2007 Share Posted March 21, 2007 1) Um, what do you mean by subroutines, and why on earth would you call main more than once? I'm guessing you want a setup something like this: In myHeader.hvoid doSomeStuff();float myComplexCalculation(float first, float second);In myProgram.cpp#include "myHeader.h"int main(char argc, char* argv[]) { ... doSomeStuff(); ... float a = 1.0f; float b = 1.0f; float x = myComplexCalculation(a, ;}// Subroutine that takes no parameters () and returns nothing (void)void doSomeStuff() { ...}// Subroutine that does a calculation on two floats and returns one floatfloat myComplexCalculation(float first, float second) { return first + second;} 2) #include should never be used for data files (with a few exceptions that I'm not going to get into; yours isn't one of them), and certainly shouldn't be used for data files that are specified at runtime. #include is used to add header files (definitions of data types and methods that you want to use; I demonstrated proper use in the above example). #include definitions are evaluated at compile-time, so specifying a data file is a bad place to do that. What do you mean by reading an excel file? What is the file saved as? If you want to read a .xls file, good luck. The file format is more-or-less proprietary and is more complicated than you want. If you're set on doing this, try looking through the source code for OpenOffice.org; this is a problem I'm sure they had to get around. If, instead, you want to read a file that's in a CSV (comma-separated value) format, which can be exported from Excel, that's easier. I'm not going to type in an example, but take a look at this, it basically spells it out for you. If I recall correctly, the >> operator used on files reads everything from the current position in the file to the next whitespace character (space, tab, newline) or comma. If it doesn't split on commas, which is what you'd need to read a CSV file, then you'll have to use the example that uses getline to read an entire line of text, and split the string apart on commas yourself. Quote Link to comment Share on other sites More sharing options...
Sir_NutS Posted March 21, 2007 Share Posted March 21, 2007 subroutines are functions I guess. SR is a qbasic/vb term so I guess that's where he coming from. which version of excel are you reading from? if it's 2007, it's xml structured so it's much more easier to open and read. If you're reading from older versions, well, good luck. There should be something around the net about it though, google it. But as Kanthos said, if you can export to CSV or any plain text format it would be much easier for you to read since you can do it with an inline read command and just parse the string. Quote Link to comment Share on other sites More sharing options...
Dhsu Posted March 21, 2007 Share Posted March 21, 2007 I think for your purposes, header files aren't really necessary. You can just put the prototypes at the top of the program. Quote Link to comment Share on other sites More sharing options...
avaris Posted March 22, 2007 Author Share Posted March 22, 2007 Thanks guys, Kanthos you are always a big help. All of my previous experience was in Basic...which is so different from C++ it's not even funny IMO. I could this program in Basic in an hour. In Basic I used subroutines for everything. The program is for an independant study in C++. I am the only person in the class, my professor assigns me programs and then I do them. Since it's an 'independant study' he refuses to teach/help me with anything. Since there isn't a standarized book or lesson plan the programs he assigns me follows, I have to come up with or figure out every function, header file, etc... for each program. He did not specify what type of excel file. All he said was there needed to be a random list of 10,000 numbers in an excel and that I would sort them using a quicksort. A CSV formatted document is def the way to go as I don't have office 2007. Thanks for the help guys I really appreciate. My uni does not offer any courses in C++ so no one there knows anything either. Well back to the grind stone. Quote Link to comment Share on other sites More sharing options...
Kanthos Posted March 22, 2007 Share Posted March 22, 2007 I think for your purposes, header files aren't really necessary. You can just put the prototypes at the top of the program. Agreed. I only did that to show proper usage Definitely go with CSV. Your prof probably said Excel because a) He can easily generate 10,000 random numbers using a short Visual Basic for Applications script within a spreadsheet and He wasn't thinking about the difficulty of reading from an Excel file. Even if the newest version saves XML-based files, you still don't want to touch that. The complexity of reading XML is higher than reading from a simple text file and is probably beyond the scope of the program you're working on right now. I'll also give you a few more pointers about subroutines. Firstly, C++ terminology never really uses subroutine. Method or function are the common terms and are used in the same way (unlike Basic where a subroutine is a block of code that doesn't return a value and a function is a block of code that does). There's no real need to distinguish between the two terms. Also think of methods in a service-oriented way. A method is a block of code that possibly takes some data as input, performs some computation, and possibly returns a result. You want to keep them as simple as possible and avoid overlap. Say you're writing a calculator program and you have a function to invert a number and a function to multiply, and you now want to write a function to divide. *Don't* write new code to do this; all you have to do is something like "return multiply(invert(x), y);". Doing it like this saves you time and saves you from running into bigger problems (if you had a bug in invert(x) and you wrote divide(x, y) by copying the code from invert and from multiply, you now have to remember to fix your bug in both places). Lastly, a note about void: you must specify void as your return type for methods that don't return a value, but the C++ convention is to not specify void for methods not taking any parameter list (i.e. you could write void doSomething(void) or void doSomething(); the convention is to do it the second way). Quote Link to comment Share on other sites More sharing options...
Tricklozen Posted March 22, 2007 Share Posted March 22, 2007 (...) an independant study in C++. You may need some reference material with introductions, then. Danny Kalev: http://www.informit.com/guides/content.asp?g=cplusplus&seqNum=4&rl=1 If you don't have it already, get the standard definition of C++ (ISO/IEC 14882:2003 should be the latest publication). Try searching for it. Bjarne Stroustrup (defined and introduced C++, read his FAQ etc) http://www.research.att.com/~bs/C++.html Quote Link to comment Share on other sites More sharing options...
Tricklozen Posted March 22, 2007 Share Posted March 22, 2007 Firstly, C++ terminology never really uses subroutine. Method or function are the common terms and are used in the same way (...) You are obviously familiar with C++ (and BASIC), but I just wanted to make it clear that the term method is never used in the C++ standard (in this manner anyway). The general term for a function that is part of a class is a member function. void aFunction () {}class aClass{ public: void aMemberFunction () {}}; Quote Link to comment Share on other sites More sharing options...
avaris Posted March 22, 2007 Author Share Posted March 22, 2007 I can't thank you guys enough for the info, it takes forever sometimes reading and trying to understand how to put together the pieces of the puzzle. Quote Link to comment Share on other sites More sharing options...
Kanthos Posted March 22, 2007 Share Posted March 22, 2007 You are obviously familiar with C++ (and BASIC), but I just wanted to make it clear that the term method is never used in the C++ standard (in this manner anyway). The general term for a function that is part of a class is a member function. Fair enough. I haven't read the C++ spec and, for that matter, haven't done any C++ programming in 2 years now. At work, it's all Java and my coworkers tend to use both terms interchangeably. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.