GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 12-Dec-2006, 09:36
Mararia Mararia is offline
New Member
 
Join Date: Dec 2006
Location: Tenerife, Canary Islands, Spain
Posts: 25
Mararia is on a distinguished road

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:
int main (){
  vector <Polinomio> P;
  ifstream (fichin);
  fichin.open ("Entrada.txt",ios::in);
  char Cadena[100];
  ofstream (fichout);
  while (!fichin.peek() != EOF){
  fichout.open("Salida.txt", ios::out | ios::app);
  fichin.getline(Cadena,100);
  switch (Cadena[0]){
    case '?':{ int k = BuscarPolin(P,Cadena[1]);
               char l = Cadena[3];
               int x = atoi(&l);
               for (int i = 1; i < 6; i++)
                fichout << Cadena[i];
               fichout <<"= ";
               fichout << P[k].Horner(x) << endl;
               break;
               };
    
    case '%':{ for (int i = 1; i < 9; i++)
                fichout << Cadena[i];
               fichout <<"= ";
               int j = BuscarPolin(P,Cadena[1]);
               int k = BuscarPolin(P,Cadena[6]);
               if (Cadena[5] == '+')
                 fichout<< P[j] + P[k]<< endl;
               if (Cadena[5] == '-')
                 fichout << P[j] - P[k]<< endl;
               if (Cadena[5] == '*')
                 fichout << P[j] * P[k]<< endl;
               break;
               };
  default:{  fichout << Cadena <<endl;
               Polinomio f;
               fichin >> f;
	       cout << "el polinomio leido es: " << f<< endl;
               P.push_back(f);
               break;
               };
   };
   fichout.close();
 };
  return 0;
}

and this is the >> operator overload:
CPP / C++ / C Code:
ifstream &operator >>(ifstream &is, Polinomio &f){
  char nom;
  string cadena;
  int co,gra,k,cont = 0;
  cont = 1;
  is >> nom;
  f.asignombr(nom);
  is.ignore(5);
  k = 0;
  while (cont != 0){
    is >> co ;
    is.ignore(2);
    is >> gra;
    if (k == 0)
      f.grado = gra;
    f.coef[gra] = co;
    k++;
    cont = gra;
  };
  return is;
}

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:
istringstream flujo (Cadena);
flujo >> f
instead of "fichin >> f;" and the same funcion I have now overloading >> but with stringstreams, not ifstreams. It hasn't worked either.

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 09:59. Reason: Please insert your C/C++ code between [cpp] & [/cpp] tags
  #2  
Old 12-Dec-2006, 14:12
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,693
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: A problem using streams. (I've spent a month trying to solve it)


Quote:
Originally Posted by Mararia
I'm new to C++

We all were new at some time (no one was born knowing this stuff).

Quote:
Originally Posted by Mararia
problems handlig Input/Output streams.

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:
Originally Posted by Mararia
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.

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:
Originally Posted by Mararia


I've used a debugger (KDBG)

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  
Old 12-Dec-2006, 17:17
Mararia Mararia is offline
New Member
 
Join Date: Dec 2006
Location: Tenerife, Canary Islands, Spain
Posts: 25
Mararia is on a distinguished road

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.
Attached Files
File Type: zip Polinomio.zip (2.6 KB, 7 views)
  #4  
Old 12-Dec-2006, 18:56
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,693
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: A problem using streams. (I've spent a month trying to solve it)


Quote:
Originally Posted by Mararia
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.

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:
ifstream & operator >>(ifstream & is, Polinomio & f)
{
    char nom;
    int co, gra, k, cont = 0;
    cont = 1;
    std::cout << "Trying is >> nom" << std::endl;
    is >> nom;
    if (is.fail()){ 
        std::cout << "1: is.fail()" << endl;
        // throw an exception or bail out or something...
    }
    std::cout << "nom: " << nom << std::endl;
    std::cout << "calling asignombr(" << nom << ")" << std::endl;
    f.asignombr(nom);
    std::cout << "calling is.ignore(5)" << std::endl;
    is.ignore(5);
    k = 0;
    while (cont != 0) {
        std::cout << "Calling is >> co" << std::endl;
        is >> co;
        if (is.fail()){ 
            std::cout << "2: is.fail()" << std::endl;
            // throw an exception or bail out or something...
        }
        std::cout << "co = " << co << std::endl;
        std::cout << "calling is.ignore(2)" << std::endl;
        is.ignore(2);
        std::cout << "calling is>> gra" << std::endl;
        is >> gra;
        if (is.fail()){ 
            std::cout << "3: is.fail()" << std::endl;
            // throw an exception or bail out or something...
        }
        std::cout << "gra = " << gra << endl;
        if (k == 0) {
            f.grado = gra;
        }
        f.coef[gra] = co;
        k++;
        cont = gra;
    };
    return is;
}

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:
Polinomio::Polinomio (int g){ //Constructor de la clase.
  grado = g;
  coef[grado]; //<--- no effect. coef[20] was declared as a class object
  for (int i = 0; i < (grado + 1); i++)
    coef [i] = 0;
  
}

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:
int BuscarPolin (vector <Polinomio> G, char name){
  for (int i = 0; i < G.size() + 1  ; i ++){
    if (G[i].nombre == name)
      return i;
  };
}

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:
CFLAGS = -c -ansi -pedantic -Wall -W

Actually, the whole makefile could be a little cleaner (simpler), but I wouldn't worry about it for now.

Regards,

Dave
  #5  
Old 13-Dec-2006, 04:43
Mararia Mararia is offline
New Member
 
Join Date: Dec 2006
Location: Tenerife, Canary Islands, Spain
Posts: 25
Mararia is on a distinguished road

Re: A problem using streams. (I've spent a month trying to solve it)


Quote:
Originally Posted by davekw7x
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.

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.)

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:
ifstream &operator >>(ifstream &is, Polinomio &f){
  char nom;
  string cadena;
  int co,gra,k,cont = 0;
  cont = 1;
  cout <<"intentando is >> nom" <<endl;
  try{
  is >> nom;
    if (is.fail()){
      cout << "1: is.fail()"<<endl;
      throw nom;
      };
  }
  catch (std::exception& nom) {
    cerr<< nom.what()<< endl;
  }
  cout << "nom: " << nom << endl;
  cout << "calling asignombr(" << nom << ")" << endl;
  f.asignombr(nom);
  cout << "calling is.ignore(5)" << endl;
  is.ignore(5);
  k = 0;
  while (cont != 0){
    std::cout << "Calling is >> co" << std::endl;
    try{
      is >> co ;
      if (is.fail()){
        cout << "2: is.fail()" <<endl;
        throw co;
      };
    }
    catch (std::exception& co) {
      cout<< co.what()<< endl;
    }
    cout << "co = " << co << endl;
    cout << "calling is.ignore(2)" <<endl;
    is.ignore(2);
    cout << "calling is>> gra" <<endl;
    try{
      is >> gra;
      if (is.fail()){
        cout << "3: is.fail()" << endl;
        throw gra;
      };
    }
    catch (std::exception& gra) {
      cout<< gra.what()<< endl;
    }
    cout << "gra = " << gra << endl;
    if (k == 0)
      f.grado = gra;
    f.coef[gra] = co;
    k++;
    cont = gra;
  };
  return is;
}

And this is how I make the program show what it's read from the input file;

CPP / C++ / C Code:
while (!fichin.peek() != EOF){
  fichout.open("Salida.txt", ios::out | ios::app);
  fichin.getline(Cadena,100);
  cout << "la cadena leida es: " << Cadena;

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:
Originally Posted by davekw7x
By the way: I don't know what happens on your system, but on mine, the third line of the constructor has no effect:

You're right, I wonder why my teacher didn't say anything when she graded that part of the program...


Quote:
Originally Posted by davekw7x
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]

Makes sense... I believe that I was thinking like with the polynomial, in that case I have use i < (grado + 1) ...


Quote:
Originally Posted by davekw7x
For me, the following is a more reasonable line in the makefile than yours
Code:
CFLAGS = -c -ansi -pedantic -Wall -W

Actually, the whole makefile could be a little cleaner (simpler), but I wouldn't worry about it for now.

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  
Old 13-Dec-2006, 08:53
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,693
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: A problem using streams. (I've spent a month trying to solve it)


Quote:
Originally Posted by Mararia
I've never used the exceptions thingies...

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:
    default:{  cout << "at switch default label" << endl;
               Polinomio f;
               cout << "Calling fichin>> f" << endl;
               fichin >> f;
               if (fichin.fail()) {
                   cout << "fichin is in the fail state" << endl;
                   break; // you could return or exit() or something here
               }
	       cout << "el polinomio leido es: " << f<< endl;
               P.push_back(f);
               break;
               }

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:
Originally Posted by Mararia
And this is how I make the program show what it's read from the input file;

CPP / C++ / C Code:
while (!fichin.peek() != EOF){
  fichout.open("Salida.txt", ios::out | ios::app);
  fichin.getline(Cadena,100);
  cout << "la cadena leida es: " << Cadena;

This is the result (click to enlarge):

Instead of an attachment, just copy/paste the text of the messages directly into the post. Note that it prints the first line from the file exactly as you told it to.

Quote:
Originally Posted by Mararia
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, i
You have read the first line and printed it out before you entered the loop where the operator was used. (I think I mentioned that your program throws away the first line, right?) Therefore the operator function is working on the second line. How could you think it would be reading the first line? It's gone, gone, gone.
Quote:
Originally Posted by Mararia
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'.

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:
  cout << "nom: " << nom << endl;
  cout << "calling asignombr(" << nom << ")" << endl;
  f.asignombr(nom);
  cout << "calling is.ignore(5)" << endl;
  is.ignore(5);
So, after printing the first char that it read from the line, you are telling it to ignore the next five chars from the second line of the file.

Here is the second line of the file:
Code:
g(x)=2x^4+1x^3-2x^2-3x^0

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:
  k = 0;
  while (cont != 0){
    std::cout << "Calling is >> co" << std::endl;
    try{
      is >> co ;
It is trying to read an int into the int variable co, right? Well since it sees an 'x', and it can't convert it to an int, the stream enters the "fail" state, as the program dutifully reports.
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:
Originally Posted by Mararia
2.- Since everything it recieves is garabage, it reads garabage and, when this data isn't of the necesary type, there's an error.

Yes. Further input on that stream is impossible unless and until you clear the error condition.
Quote:
Originally Posted by Mararia
Quote:
Originally Posted by davekw7x
By the way: I don't know what happens on your system, but on mine, the third line of the constructor has no effect:

You're right, I wonder why my teacher didn't say anything when she graded that part of the program...

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:
    coef[grado];
Your class definition declared coef to be an array of 20 ints, right. The expression coef[grado] is evaluated. The value of the expression is whatever is in memory at the location addressed by coef[grad]. But nothing is done with the expression, so it is not useful. NOte that if the argument 'g' is greater than or equal to 20, the address is actually beyond the range of the array, and programs are not supposed to access such things. But there is (usually) no harm in reading them, although actual program behavior is indeterminate. See footnote.

Quote:
Originally Posted by davekw7x
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]
Quote:
Originally Posted by Mararia
Makes sense... I believe that I was thinking like with the polynomial, in that case I have use i < (grado + 1) ...

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:
Originally Posted by Mararia
Quote:
Originally Posted by davekw7x
For me, the following is a more reasonable line in the makefile than yours
Code:

CFLAGS = -c -ansi -pedantic -Wall -W


Actually, the whole makefile could be a little cleaner (simpler), but I wouldn't worry about it for now.
I didn't make the makefile

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  
Old 14-Dec-2006, 02:29
Mararia Mararia is offline
New Member
 
Join Date: Dec 2006
Location: Tenerife, Canary Islands, Spain
Posts: 25
Mararia is on a distinguished road
Thumbs up

Re: A problem using streams. (I've spent a month trying to solve it)


Quote:
Originally Posted by davekw7x
Instead of an attachment,
Actually, it wasn't an attachment, it was a link to a website where I uploaded the picture so it wouldn't take space in the forum...

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:
default:{ cout << "Estamos en default de la sentencia switch" << endl;
               fichout << Cadena <<endl;
               Polinomio f;
	       istringstream flujo (Cadena);
               flujo >> f;

//and the operator is now:
istringstream &operator >>(istringstream &is, Polinomio &f){//nothing's changed here since yesterday.. well, yes, somethings changed, now I can count :lol}

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  
Old 14-Dec-2006, 09:09
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,693
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: A problem using streams. (I've spent a month trying to solve it)


Quote:
Originally Posted by Mararia
Actually, it wasn't an attachment, it was a link to a website where I uploaded the picture so it wouldn't take space in the forum...
Then, change my comment to: Just paste the text messages into your post; don't make a link to some website, since many of our members simply will not go to unknown places (and it's inconvenient for the ones that do).
Quote:
Originally Posted by Mararia
Well, I made some progress... Now I can read the polynomials
.
.
.
OK!!!! Using stringstreams seems like a good way to be able to read a line from the file into a buffer then reread it, depending on the first character in the line. Everything seems to be coming together.

Quote:
Originally Posted by Mararia
Now, my main problem ... is that it always goes to default.
I question your conclusion.
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:
  cout << "After fichin.getline, before switch:: Cadena<" << Cadena << ">" << endl;
  switch (Cadena[0]) {

    case '?':{ cout << "Case '?':" << endl;
               int k = BuscarPolin(P,Cadena[1]);
.
.
.
               fichout << P[k].Horner(x) << endl;
               break;
               }
    
    case '%':{ cout << "Case '%':" << endl;
               for (int i = 1; i < 9; i++)
.
.
.
              break;
               }
  default:{ cout << "Estamos en default de la sentencia switch" << endl;
               cout << "Cadena: <" << Cadena << ">" << endl;
.
.
. 
              flujo >> f;
               break;
          }

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  
Old 14-Dec-2006, 16:52
Mararia Mararia is offline
New Member
 
Join Date: Dec 2006
Location: Tenerife, Canary Islands, Spain
Posts: 25
Mararia is on a distinguished road

Re: A problem using streams. (I've spent a month trying to solve it)


Finally, it works!!!

I did this:
Quote:
Originally Posted by davekw7x
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:

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 GIDBlogWelcome to Baghdad by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Please Help Me To Build My Calendar!!! suriacute85 Java Forum 0 05-Oct-2006 19:39
1st month free on shared,,discounted 1st month on resellers!! my-e-space Web Hosting Advertisements & Offers 0 26-Dec-2005 01:11
Hitting a logic problem I can't solve Elsydeon C++ Forum 3 09-Oct-2005 21:29
C++ Program Print Calendar HELP tones1986 C++ Forum 2 08-Sep-2005 21:05

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 13:13.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.