pseudo-fehler in funktion



  • als ergebnis meiner funktion bekomm ich eine positive zahl obwohl sie rechnerisch negativ sein muss:

    ...
    
    long ziffer(char c) {
      long zahl = static_cast<long>(c) - static_cast<long>('0');
      return zahl;
    }
    
    long gziffer(vector<char> vc) {
      int x = 0;
      long zerg = 0;
    
      while(x < vc.size()) {
    
        if(isdigit(vc[x])) {
          zerg = zerg * 10 + ziffer(vc[x]);
          x++;
        }
        if(vc[x] == '+') {
          zerg * 10 + ziffer(vc[x]);
          x++;
        }
        if(vc[x] == '-') {
          x = 0;
          -(zerg * 10) - ziffer(vc[x]);     // zerg ist immer positiv
          x++;
        }
    
      }
      cout<<endl<<zerg;
      return zerg;
    }
    
    ...
    

    weiß irgendjemand einen rat? thx!



  • ceberus schrieb:

    zerg * 10 + ziffer(vc[x]);
          -(zerg * 10) - ziffer(vc[x]);
    

    Diese Zeilen tun nichts, solange du das Ergebnis des Ausdrucks nicht weiterverarbeitest, z.B. durch eine Zuweisung.



  • wenn du eine zuweisung ala: zerg = zerg * 10 + ziffer(vc[x]); meinst, damit hab ichs auch nicht hinbekommen.



  • Hi

    long gziffer(vector<char>& vc) 
    {
      int x = 0;
      long zerg = 0;
    
      bool is_nega = false;
    
      if(vc.size() > 1) 
        is_nega = ((vc[0] == '-')? true : false);
    
      while(x < vc.size()) 
      {
        if(isdigit(vc[x])) 
          zerg = zerg * 10 + ziffer(vc[x]);
        ++x;
      }
    
      if(is_nega) zerg *= (-1);
    
      cout<<endl<<zerg<<endl;
      return zerg;
    }
    


  • es funktioniert! thx (:


Anmelden zum Antworten