- Ad -
IMPRESSUM dotted_line KONTAKT dotted_line search dotted_line Contact dotted_line sitemap
191 PHP-Resource Mitglieder online
php-resource



Archiv verlassen und diese Seite im Standarddesign anzeigen :
C++ Fehler


 
Dave McClan
21-05-2007, 22:01 
 
Hallo,
Ich habe ein kleines Problem mit C++.

Beim Kompilieren mit GCC bekomme ich folgenden fehler:

file.cpp: In function 'int main()':
file.cpp:5: error: 'string' was not declared in this scope
file.cpp:5: error: expected `;' before 'username'
file.cpp:6: error: expected `;' before 'command'
file.cpp:7: error: 'username' was not declared in this scope
file.cpp:12: error: 'command' was not declared in this scope


Mei Code sieht so aus:


#include <iostream>

int main() {
string username;
string command;
username = "nicolas";

std::cout << username;
std::cout << "$: ";

std::cin >> command;

std::cout << command;
std::cout << "text_command";

return 0;
}


Ich hoffe es kann mir wer helfen,

Gruß

 
penizillin
21-05-2007, 22:09 
 
wie wärs mit #include <string> ?

 
onemorenerd
21-05-2007, 22:16 
 
und using namespace std; ?

 
Dave McClan
22-05-2007, 08:27 
 
Vielen Dank an euch,
Nun funktioniert es!

Danke

EDIT:// Ich hab noch ein kleine Frage an euch.
Wenn man nun was eingibt, wird es angezeigt und das Programm beendet.
Kann man es irgendwie machen das nach dieser Frage, die gleiche Frage nochmal kommt und man nur mit "exit" aus dem Programm kommt?


Gruß

 
penizillin
22-05-2007, 10:31 
 
while(command != "exit"){
cin >> command;

cout << "eingabe war: "<<command << "\n";
}

 
closure
22-05-2007, 10:49 
 
Original geschrieben von onemorenerd
und using namespace std; ?

nee lieber nicht den ganzen namespace importieren.


#include <iostream>
#include <string>

int main(){
std::string username("nicolas"),command;
std::cout << username << "$: ";
std::getline(std::cin,command);
std::cout << command << "text_command" << std::endl;
}


das explizite return brauchst du nicht, da der standard ein implizites
return vorsieht. Mit std::getline kannst du auch kommandos einlesen
die mehr als ein "wort" umfassen. Ansonsten fehlt dem ding jegliche
funktionalität ;)

greets

nachtrag:
für eine einfache schleife wie du sie möchtest reicht folgendes.
(aber aufpassen, string_to_lower weiss nichts von locales)

#include <iostream>
#include <string>

inline std::string string_to_lower(const std::string &str){
std::string out(str);
std::transform(out.begin(),out.end(),out.begin(),
static_cast<int(*)(int)>(std::tolower));
return out;
}

int main(){
std::string prompt("nicolas$: "),command;
while(string_to_lower(command) != "exit"){
std::cout << prompt;
std::getline(std::cin,command);
//mach was
}
}

 
Dave McClan
22-05-2007, 12:54 
 
Vielen Dank closure!

Jetz funktioniert es, danke für alle und an alle!


Gruß


Alle Zeitangaben in WEZ +2. Es ist jetzt 23:42 Uhr.