Soße: c++ prob

Halle,

ich habe ien einfaches programm geschrieben:

include <iostream.h>

void main (void)
{
char x;
char pass="hallo";

cout<<"Passwort: ";
cin>>x;

if (x == pass)
{
cout<<"ja";
}
else
{
cout<<"ne";
}

}

gibt der anwender hallo ein kommt trotzdem ne als ausgabe raus - warum ?????

wenn da jetzt tipp fehler sind, dann sorry das alte prog hab ich net gefunden, aber allein von der logik her müsste es doch klappen.

Danko

  1. Halle,

    ich habe ien einfaches programm geschrieben:

    include <iostream.h>

    void main (void)
    {
    char x;
    char pass="hallo";

    cout<<"Passwort: ";
    cin>>x;

    if (x == pass)
    {
    cout<<"ja";
    }
    else
    {
    cout<<"ne";
    }

    }

    gibt der anwender hallo ein kommt trotzdem ne als ausgabe raus - warum ?????

    wenn da jetzt tipp fehler sind, dann sorry das alte prog hab ich net gefunden, aber allein von der logik her müsste es doch klappen.

    eigentlich schon, allerdings hat die Eingabe sicherlich ein Enterzeichen oder Null(Ende)zeichen hintendran und damit ist der Vergleich nicht stimmig
    Benutze strcmp(str1, str2) (also strcmp(x,pass);) und wenn das immernoch nicht funzt muss du mal alle leerzeichen und enterzeichen vorher noch abschneiden von deinem Eingabe-string
    aussedem glaube ich mich zu erinnern, dass man ..
    moment sorry gerade fällts mir ein
    hast du schon mal ein cout<<pass gemacht, da dürfte nur h drinstehen und h != hallo ...
    cin.getline() sollte das sein was du brauchst um einen string einzulesen
    versuchs mal

    Danko

    bitte romy

    1. ne ne es leigt 100pro an der if else struktur, obwohl gerade die ok ist, denn
      ich hab das mal so gemacht char x,y
      und cin für x und y und hab jedesmal bei x und y hallo eingegeben, so das x hallo ist und y hallo (oder eben hallo\0 bei beiden)
      mit der selben if else struktur und es kommt ne raus !!!!

      hhmmmmm
      das mit dem string copy ist schon wieder sowas was nicht enfach ist-ich kann es aber es soll besnders einfach sein !

      1. ne ne es leigt 100pro an der if else struktur, obwohl gerade die ok ist, denn
        ich hab das mal so gemacht char x,y
        und cin für x und y und hab jedesmal bei x und y hallo eingegeben, so das x hallo ist und y hallo (oder eben hallo\0 bei beiden)
        mit der selben if else struktur und es kommt ne raus !!!!

        jaja, aber ich ahtte mich doch noch korrigiert

        mach bitte nach dem cin ein cout der gerade gelesenen variable und sag mir was drinsteht
        nicht zufällig bloss h
        ich denke du brauchst cin.getline();

        hhmmmmm
        das mit dem string copy // string compare meinte ich

        ist schon wieder sowas was nicht enfach ist-ich kann es aber es soll besnders einfach sein !
        es ist einfach

        if(!strcmp(x,pass)) cout<<"ja";
        else cout<<"ne";

        <anmerkung> strcmp gibt Null zurück, wenn die strings gleich sind </anmerkung>

        romy

        1. in x und y steht hallo wenn ich es ausgebe

        2. Hallo,

          if(!strcmp(x,pass)) cout<<"ja";
          else cout<<"ne";

          <anmerkung> strcmp gibt Null zurück, wenn die strings gleich sind </anmerkung>

          romy

          Hast Du schon bemerkt, dass x und pass bei Soße keine Strings sind, sondern Chars. Ne, auch keine Char-Arrays, sondern einfache Chars?

          Gruß

          Axel

        3. Poste mal ei einfaches alternativ script/prog !

          1. Poste mal ei einfaches alternativ script/prog !

            sorry kriegs nicht hin, da ich es nicht kompiliert kriege
            (hab c++ nur mit windows bearbeitet, nie mit Linux)

            Axel hat aber erstmal recht, das du character benutzt und keine Strings
            mit fällt nicht mehr ein wie die Bibliothek hies, die man da einbinden muss um strings zu benutzen

            wenn du noch n tag zeit hast, würde ich es zu Hause nochmal probieren und dir dann posten bzw. ne mail schicken

            vielleicht hat ja auch schon jemand anderes eine Lösung

            sorry
            romy

            meine Fehlermeldungen:
            aufruf von
            gcc -Wall -O test.cpp -o test
            /tmp/ccvC1X1W.o: In function main': /tmp/ccvC1X1W.o(.text+0xf): undefined reference to cout'
            /tmp/ccvC1X1W.o(.text+0x14): undefined reference to ostream::operator<<(char const \*)' /tmp/ccvC1X1W.o(.text+0x20): undefined reference to endl(ostream &)'
            /tmp/ccvC1X1W.o(.text+0x2f): undefined reference to cin' /tmp/ccvC1X1W.o(.text+0x34): undefined reference to istream::operator>>(char &)'
            /tmp/ccvC1X1W.o(.text+0x4a): undefined reference to cout' /tmp/ccvC1X1W.o(.text+0x4f): undefined reference to ostream::operator<<(char const *)'
            /tmp/ccvC1X1W.o(.text+0x5b): undefined reference to endl(ostream &)' /tmp/ccvC1X1W.o(.text+0x6a): undefined reference to cout'
            /tmp/ccvC1X1W.o(.text+0x6f): undefined reference to ostream::operator<<(char const \*)' /tmp/ccvC1X1W.o(.text+0x7b): undefined reference to endl(ostream &)'
            collect2: ld returned 1 exit status
            das verstehe ich nicht

            1. guck dir das mal an

              http://www.juicystudio.com/tutorial/cpp/strings.html

              bei mir kann es auch daran liegen, dass ich den richtigen aufruf zum kompilieren nicht hinkriege, da der zwar für c -> gcc ist, aber vielleicht anders für c++
              naja, merke jedenfalls, das man viel vergisst wenn man sich nicht mehr damit beschäftigt

              ciao romy

              1. Halle nochmal,

                erstmal danke für dein angebot !
                wenn du ne lösung hast dan poste sie doch unter nem neuen thread -> betreff: "@soße: C++ Prob" oder so !!?

                Danko