![]() |
|
|||||||
|
|
Thread Tools | Search this Thread | Rate Thread |
|
#1
|
|||
|
|||
A problem using streams. (I've spent a month trying to solve it)Hi! I'm new to C++ and I'm having problems handlig Input/Output streams. I have to read a file, in that file, I'll find a polynomial expression (i.e: 3x^2 -x+5) or an operation (i.e: f(x) + g(x) , f(3)). My problem is that I can't read the polynomial expression.
This is my main function: CPP / C++ / C Code:
and this is the >> operator overload: CPP / C++ / C Code:
I've used a debugger (KDBG) and I've managed to find out that is >> gra, is >> co, does nothing to these variables. I thought that doing is >> 'variable' would get a value out of the stream and into the variable. But I looked at the locals in every line and they never changed. I've also tried : CPP / C++ / C Code:
The idea for the polynomial expression is to store it into an Array, where the coefficient would go into its grade's slot. In the example before, 3 would go into slot[2], -1 into slot[1] and 5 into slot [0]. Well, I'd appreciate any and all the help you can offer. And sorry if I've made any mistakes writing this, but English is not my mother language. Thanks. Last edited by LuciWiz : 12-Dec-2006 at 10:59.
Reason: Please insert your C/C++ code between [cpp] & [/cpp] tags
|
|||
|
#2
|
||||
|
||||
Re: A problem using streams. (I've spent a month trying to solve it)Quote:
We all were new at some time (no one was born knowing this stuff). Quote:
So, have you done any previous programs with iostreams? Are you absolutely certain that you can read and write the types of variables using the functions that your present program is attempting? Make simple examples with simple variables before jumping into operator overloading for classes. Quote:
Here's the thing: I don't see how we can help you get to the bottom of things unless we see the code that defines and implements the class. (In other words: Post the Code --- At least post the complete class definition files). Secondly, show us exactly what the contents of input file is, so that we may have some clue as to what your program was actually working on. your code seems to be looking for certain specific things in certain specific columns; there is no way (no way) that I can see how to help unless I can make a program that sees what your program sees. Quote:
Debuggers are wonderful tools, but for people who are "new to C++" there are some other very important tools: 1. The program itself. 2. Your brain. Look at the code. If you think it's OK, then make the program tell you what it is working on at each step (put print statements to show values of variables at various stages of the program). In other words, at points where your program is reading the input file, put in some temporary statement to print out the entire line that it just read. If you don't understand what the program is telling you, then post your questions. Tell us what you expected to see and tell us what the program told you. Tell us what you don't understand about the difference. The problem with using a debugger is that no one can tell exactly what you did just by having you tell us what you did (or what you thought you did). With program print statements, we can try to duplicate your setup and your problem, and, maybe --- just maybe --- we can make some suggestions to help you see how to debug your program. Regards, Dave |
|
#3
|
|||
|
|||
Re: A problem using streams. (I've spent a month trying to solve it)Um... the whole code is a bit too long.. They're three source codes, the makefile and the input file. So, if you don't mind, l'll attach a .zip containing everything.
Yes, we have overloaded other iostreams... In fact, I know that the class works because before this, we entered the polynomials by keyboard (you'll see how I overloaded 'cin', it's commented so it became deactivated just in case it could cause any conflicts). And, to make matters worse, my classmates have been succesfull overloading this like I am (I've even resorted to copying how they do it with no luck) I know where the mistake is. But I don't know why it happens or how to fix it. The problem is, like I said, that 'is >> co' and 'is >>gra' don't change their values. It's as if 'is' were 'empty' (makes sense?) and thus, it doesn't store anything into these variables. I saw this with the debugger and, to check it, I made 'cout << co' and 'cout << gra' before and after 'is >> gra' and 'is >> co'. The values were the same before and after... Thanks for your help. PS: If uploading the .zip file goes against any rule, tell me so and I'll paste the code and take the file away. |
|
#4
|
|||
|
|||
Re: A problem using streams. (I've spent a month trying to solve it)Quote:
Print out each line that you read in your main program (right after getline(), print it out.) It looks like you throw the first line away, but of course that isn't the real problem. Put std::cout statements in your overloaded >> operator. There is something terribly wrong with your inputs. It seems to fall apart somewhere around the time it tries to read co. Put if (is.fail()) after each attempt to read from is. You really should do that anyhow (that is, make the operator throw an exception if the input data are not formatted correctly). After you get the basic polynomial stuff working, in my opinion you really should make the input more forgiving of spacing. But in any case, you must always (always) check that you are getting valid input. If you get a bad something somewhere you can either exit the program, or print an error message and have the code structured so that it will try to read the next line or whatever you thing appropriate. In the meantime, make the program tell you what it is working on at each step and let you see where it goes wrong. CPP / C++ / C Code:
Once the ifstream enters the "fail" state, you can do nothing more with it unless you clear it and empty the input buffer. Otherwise, futher attempts to read from it are ignored. (So that's why the debugger showed that nothing was happening with co and gra. The debugger led you to the problem area, but it's up to you to discover why there is a problem. My philosophy: Make the program tell you what the problem really is.) By the way: I don't know what happens on your system, but on mine, the third line of the constructor has no effect: (If you want to allocate storage in the constructor, you use can use new[]. Just don't forget to use delete[] in the destructor.) CPP / C++ / C Code:
Also, the following function could return without a return value (if it runs out of the loop). This is a Bad Thing. Also, it should be i < G.size(), not i < G.size() + 1, since legal index values are [0], [1], ... [G.size()-1] CPP / C++ / C Code:
I didn't check other code validity, but you should at least turn on all possible compiler warnings. For me, the following is a more reasonable line in the makefile than yours Code:
Actually, the whole makefile could be a little cleaner (simpler), but I wouldn't worry about it for now. Regards, Dave |
|
#5
|
||||
|
||||
Re: A problem using streams. (I've spent a month trying to solve it)Quote:
Well I've tried everything. Here is the new operator overload (I've never used the exceptions thingies... I didn't even know they existed... So I've probably done something wrong) CPP / C++ / C Code:
And this is how I make the program show what it's read from the input file; CPP / C++ / C Code:
This is the result (click to enlarge): http://img228.imageshack.us/my.php?image=erroresir3.jpg My thoughts about these results: 1.- It reads the first line but the operator recieves either the second line (because it assigns g to nom; but I don't think so, if it did, it wouldn't have problems reading it) or it recieves part of the second line and some junk or it recieves junk and its a coincidence that the first thing in this junk is the character 'g'. 2.- Since everything it recieves is garabage, it reads garabage and, when this data isn't of the necesary type, there's an error. Quote:
You're right, I wonder why my teacher didn't say anything when she graded that part of the program... Quote:
Makes sense... I believe that I was thinking like with the polynomial, in that case I have use i < (grado + 1) ... Quote:
I didn't make the makefile (I only made the clean2 and open rules) , I just downloaded it from the teachers' website and change the names of the files in every new program. |
|
#6
|
|||||||||||
|
|||||||||||
Re: A problem using streams. (I've spent a month trying to solve it)Quote:
Actually, it looks to me as if the exception works perfectly. It printed the message at the point where the stream failed and then aborted the program. But, forget about exceptions for now. My intent was to emphasize that you must test validity of input data as much as possible. For purposes of debugging, you can put extra print stuff to show the program's progress. Whether or not you do the exception thing in the >> function, in your main program function, you can test the state of the stream after the input function: CPP / C++ / C Code:
In this case, it never gets to this part, since the program aborts while it is still inside the >> function, but I think that you should always test results after any input. Quote:
Quote:
Quote:
But it IS having problems reading it. And, believe me, it's not a coincidence! (Better yet, don't believe me, test it yourself.) Since the output statement tells you that the first thing on the line is 'g' let's look at the code and see how the heck it could have problems reading the other stuff on the second line. Now look (yes, look) at the code after it reads the first thing from the second line: CPP / C++ / C Code:
Here is the second line of the file: Code:
Now, write this down on a piece of paper. Put your pencil on the first thing (the 'g'). Skip five chars. I'll do it with you. Where is your pencil now? Mine is on the 'x'. What is the next thing that your program does? CPP / C++ / C Code:
STOP. Right now. There is a bug that causes the routine to fail to read the second line properly. FIX IT. You have made the program tell you what is happening. There can be no doubt that the stream is in the "fail" state. The program can not tell you how to fix the program (neither can the debugger). YOU must fix the program. Quote:
Yes. Further input on that stream is impossible unless and until you clear the error condition. Quote:
Actually I wasn't being completely truthful here. (I kind of wanted you to look at the code and see why I would say that it has no effect.) I know exactly what happens here in the constructor: CPP / C++ / C Code:
Quote:
Quote:
The vector class member function size() tells the number of elements in the vector. If there are n elements, then they can be accessed with G[0], G[1], ... G[n-1], just like elements in a simple array. Note that a polynomial of degree n has n+1 coefficients, so the size of the vector would be n+1. My point is that it doesn't matter how the stuff got into the vector or what the vector represents, the size() function tells you how many elements there are in the vector. Period. Don't overthink. Quote:
OK. Ignore my comment about "could be cleaner"; that's a matter of style, and It doesn't help you to have me comment on the style that is on your instructor's web site. But at least please, please put in the extra warnings "-W and -Wall". This will find some potential problem areas for you to help you in your efforts.I understand it if you didn't know about the warning flags before. But now you know. Let the compiler give as much help as it can. Once you get past this problem (getting the overloaded >> operator to do the Right Thing), your program has lots of opportunity for learning how to debug (by that I mean that there are lots of problems that you will have to solve). Make the program tell you what is happening at each step where something questionable occurs. The, look at the code. Regards, Dave Why the heck would you use an array of char instead of a std::string here? (That's a rhetorical question; I don't want you to tell me. I just mention it as a possibility for your further consideration. This assignment is defining a user class and then operating on a std::vector of these objects. Surely you have had std::strings, right?) |
|
#7
|
|||
|
|||
Re: A problem using streams. (I've spent a month trying to solve it)Quote:
Well, I made some progress... Now I can read the polynomials ... I still don't know exactly where I was throwing the first line away, so I did this:CPP / C++ / C Code:
Now, my main problem (I expected it, because it's happening in another program too) is that it always goes to default. I can't make it enter the other cases. I guess it's because I'm comparing wrongly, but I haven't been able to find out exactly what I'm doing wrong... Mr. google has showy me lots of examples of the switch sentence comparing characters and I can't figure what's different between those and mine... |
|
#8
|
|||
|
|||
Re: A problem using streams. (I've spent a month trying to solve it)Quote:
Quote:
Quote:
I can't see anything wrong with your switch statement or the way that you are using it. If it were my program (it's not my program; it's yours) and if I were trying to debug it (I'm not going to debug it; you are), here is what I would do: 1. Put a print statement just before the switch statement so that I could see what the switch is actuallly working on. 2. Put print statements in each case of the switch statement to make absolutely sure that the program is working on what I think it should be: CPP / C++ / C Code:
Then, see what happens next. See how it works? Don't guess about what you are doing wrong; make the program tell you. You can find out what is happening at each step by making the program tell you. If it tells you something other than what you expected, then you can try to figure out why. If you can't see any logical problems with your code, maybe you need some other print statements to tell you something else about what the program is doing (and why). Don't just get to a place where it misbehaves and then give up. Debugging is a learning process, as much as writing code. Use your own logic (your brain) and use the program itself to get to the bottom of things. If you can't see it, then 1. Post the code 2. Tell us exactly what the program output was. 3. Tell us what you expected the program output to be. 4. Tell us what you don't understand about the difference between 2 and 3. Regards, Dave |
|
#9
|
|||
|
|||
Re: A problem using streams. (I've spent a month trying to solve it)Finally, it works!!!
![]() I did this: Quote:
I wouldn't enter in the different cases. I don't know how or why I thought of cleaning the directory, but I did. I compiled again and executed and got the desired results. And, what's more, thanks to this advice, I managed to make the other program (the one I said it was giving me problems with the switch sentence) work too. Now, I 'only' have to catch up writing 4 or 5 programs... Thanks for everything, I'll come back again, I'm sure. See you! |
Recent GIDBlog
Accepted for Ph.D. program by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Please Help Me To Build My Calendar!!! | suriacute85 | Java Forum | 0 | 05-Oct-2006 20:39 |
| 1st month free on shared,,discounted 1st month on resellers!! | my-e-space | Web Hosting Advertisements & Offers | 0 | 26-Dec-2005 02:11 |
| Hitting a logic problem I can't solve | Elsydeon | C++ Forum | 3 | 09-Oct-2005 22:29 |
| C++ Program Print Calendar HELP | tones1986 | C++ Forum | 2 | 08-Sep-2005 22:05 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The