Problem mit Texteingabe

betaros

betaros

Computerspezi
Moin,

ich programmiere grade in C++ ein Programm in dem man Profile anlegen kann und stehe noch ziemlich am Anfang :)

Code:
#include <iostream>
#include <string>
#include <fstream>
#include <stdio.h>
#include <string.h>
using namespace std;

int menu(int menutype);
int profiles(int profilemenu);

int main(){
	int status,menutype = 0;
	status = menu(menutype);
	if(status == 1){
		cout << "Action failed! Maybe wrong Input?" << endl;
		return 1;	
	}
	return 0;
}

int menu(int menutype){
	switch(menutype){
		default: return 1;
		case 0:
			int input, status;
			cout << "Monex 0.01" << endl;
			cout << "----------" << endl;
			cout << "1) Create Profile" << endl;
			cout << "2) Load Profile" << endl;
			cout << "3) Delete Profile" << endl;
			cin >> input;
			status = profiles(input);
			if(status == 0){
				cout << "Action was successful!" << endl;
				return 0;
			} else {
				cout << "Action failed! Maybe wrong Input?" << endl;
				return 1;	
			}
		break;
		}
		return 1;
	}
	
int profiles(int profilemenu){
	switch(profilemenu){
		default: return 1;
		case 1:
			ofstream create_profile("1.txt");
			string correct;
			do{
			char fullname[256], address[256], city[256], country[256];
			cout << "Full Name: ";
			cin.getline(fullname,256);
			cout << "Address:   ";
			cin.getline(address,256);
			cout << "City:      ";
			cin.getline(city,256);
			cout << "Country:   ";
			cin.getline(country,256);
			cout << "Sie heißen " << fullname << " und kommen aus " << address << ", " << city << ", " << country << ". Ist das korrekt? (J/N)";
			cin >> correct;
			if(correct == "J" || correct == "j"){
				char buffer[1028];
				strcat(buffer,fullname);
				strcat(buffer,":");
				strcat(buffer,address);
				strcat(buffer,":");
				strcat(buffer,city);
				strcat(buffer,":");
				strcat(buffer,country);
				strcat(buffer,"#");
				cout << buffer << endl;
				create_profile.write(buffer,1028);
				create_profile.close();
				return 0;
			}}while(correct=="N"||correct=="n");
		}
		return 1;
	}

Mein Problem erkennt man wahrscheinlich bei der Ausführung:
Code:
[betaros@Chef ~]$ ./Entwicklung/monex/main 
Monex 0.01
----------
1) Create Profile
2) Load Profile
3) Delete Profile
1
Full Name: Address:   Zuhause
City:      Hier
Country:   Am Arsch der Welt
Sie heißen  und kommen aus Zuhause, Hier, Am Arsch der Welt. Ist das korrekt? (J/N)j
�K:Zuhause:Hier:Am Arsch der Welt#
Action was successful!

Ich kann Fullname nicht angeben :( Deshalb kommt am Anfang der Ausgabe "�K". Wie kann ich das Problem beheben?

mfg
betaros
 
Code:
#include <iostream>
#include <string>
#include <fstream>
#include <stdio.h>       [B]// <= solltest du in einem C++ Programm vermeiden, wenns schon sein muss cstdio[/B]
#include <string.h>      [B]// ditto[/B]
using namespace std;

int menu(int menutype);
int profiles(int profilemenu);

int main(){
        int status,menutype = 0;
        status = menu(menutype);
        if(status == 1){
                cout << "Action failed! Maybe wrong Input?" << endl;
                return 1;
        }
        return 0;
}

int menu(int menutype){
        switch(menutype){
                default: return 1;
                case 0:
                        int input, status;
                        cout << "Monex 0.01" << endl //die vielen cout kannst du dir sparen
                               << "----------" << endl;
                        cout << "1) Create Profile" << endl;
                        cout << "2) Load Profile" << endl;
                        cout << "3) Delete Profile" << endl;
                      [B]  //input ist ein int, das '\n' bleibt im stdin[/B]
                        cin >> input;
                        status = profiles(input);
                        if(status == 0){
                                cout << "Action was successful!" << endl;
                                return 0;
                        } else {
                                cout << "Action failed! Maybe wrong Input?" << endl;
                                return 1;
                        }
                break;
                }
                return 1;
        }

int profiles(int profilemenu){
        switch(profilemenu){
                default: return 1;
                case 1:
                        ofstream create_profile("1.txt");
                        string correct;
                        do{
                        char fullname[256], address[256], city[256], country[256];
                        cout << "Full Name: ";
                        [B]//das \n von input wird eingelesen[/B]
                        cin.getline(fullname,256);
                       [B] // fullname ist 256 gross, du brauchst platz für das '\0'
                        // ein C++ string wär da natürlich schöner[/B]
                        cout << "Address:   ";
                        cin.getline(address,256);
                        cout << "City:      ";
                        cin.getline(city,256);
                        cout << "Country:   ";
                        cin.getline(country,256);
                        cout << "Sie heissen " << fullname << " und kommen aus " << address << ", " << city << ", " << country << ". Ist das korrekt? (J/N)";
                        cin >> correct;
                      [B]  //bei sowas match immer auf 'j', nicht auf "j", "j" ist ein null terminierter string, 'j' ist der platz der ansi tabelle an der 'j' steht[/B]
                        if(correct == "J" || correct == "j"){
                                char buffer[1028];
                                strcat(buffer,fullname);
                                strcat(buffer,":");
                                strcat(buffer,address);
                                strcat(buffer,":");
                                strcat(buffer,city);
                                strcat(buffer,":");
                                strcat(buffer,country);
                                strcat(buffer,"#");
                                cout << buffer << endl;
                                create_profile.write(buffer,1028);
                                create_profile.close();
                                return 0;
                        }}while(correct=="N"||correct=="n");
                }
                return 1;
        }

Ich war jetzt nicht motiviert das ganze selber zu kompilieren, ich hoff aber doch das die kommentare dir weiterhelfen.
 
Vielen Dank für die schnelle Hilfe :)
nun habe jetzt die Bibliotheken am anfang geändert und bin grade dabei aus
Code:
cin >> input;
status = profiles(input);
ein
Code:
cin >> s_input;
input = atoi(s_input.c_str);
status = profiles(input);
zu machen, wobei s_input ein String ist. Beim kompilieren, sagt er mir dann folgendes:
Code:
main.cpp:34:30: Fehler: Argument des Typs »const char* (std::basic_string<char>::)()const« passt nicht zu »const char*«

Habe ich dich vielleicht auch nur falsch verstanden?

betaros
 
strin::c_str() ist eine funktion, das was du probierst ist vermutlich

Code:
input = atoi(s_input.c_str());

Es spricht eh nichts dagegen ein int einzulesen, nur du solltest zb

Code:
cin >> input;
cin.ignore(1, '\n')

machen, das du das '\n' aus dem buffer rausbekommst.

Die Anmerkung mit den Strings war eher auf deine char arrays bezogen, hässlich sowas :oldman
 
Nochmal vielen Dank! Jetzt ist das Problem verschwunden :)

Ich habe jetzt aus den chars strings gebaut und wieder ein Problem: create_profile.write() benötigt chars (deshalb diese char Lösung) Da ich aber nur Strings habe, gibt er mir natürlich Fehler. Wenn du du mir das auch noch machst, bin ich dir unendlich dankbar :D

betaros

//Edit:

Problem gelöst:
Code:
int profiles(int profilemenu){
	switch(profilemenu){
		default: return 1;
		case 1:
			ofstream create_profile("1.txt");
			string correct;
			do{
			string fullname, address, city, country;
			cout << "Full Name: ";
			getline(cin,fullname);
			cout << "Address:   ";
			getline(cin,address);
			cout << "City:      ";
			getline(cin,city);
			cout << "Country:   ";
			getline(cin,country);
			cout << "Sie heißen " << fullname << " und kommen aus " << address << ", " << city << ", " << country << ". Ist das korrekt? (J/N)";
			cin >> correct;
			if(correct == "J" || correct == "j"){
				string buffer = fullname + ":" + address + ":" + city + ":" + country + "#";
				cout << buffer << endl;
				const char* c_buffer = buffer.c_str();
				int l_buffer = buffer.length();
				create_profile.write(c_buffer,l_buffer);
				create_profile.close();
				return 0;
			}}while(correct=="N"||correct=="n");
		}
		return 1;
	}
 
Zuletzt bearbeitet:

Ähnliche Themen

Ausführbare C-Datei von Mac OS auf Embedded Linux ausführen

Samba 4 Gast Zugang unter Ubuntu funktioniert nicht

Akonadi startet nicht mehr

dynamische Speicherreservierung

Displayport + externer Monitor zeigt bei startx nichts erst bei DVI

Zurück
Oben