c++ Rechenprogramm mit einem Fehler beim zuweisen



  • Moin zusammen!

    Ich schreibe mir für die Schule ein kleines (Eingabeaufforderung/Dos) Programm. In diesem wird der wert einer Variable (ein Teil davon) nicht korrekt in eine andere Variable abgespeichert (der Inhalt der Variable "input" hat zum Beispiel den Inhalt "4x4-3x3+2x+x-5" oder auch "3x9-4x4+2x2")

    Der Code ist folgender:

    int full_curve_investigation_symmetry() {
    
    	cout << endl;
    	analyse_loop2=0;
    
    	for(analyse_loop=0;analyse_loop<=string_length;analyse_loop++) {
    
    		if(input[analyse_loop]=='x') {
    
    			exponent_number[analyse_loop2]=input[analyse_loop+1];
    
    			cout << exponent_number[analyse_loop2] << endl;  // hier wird ein falscher Wert angezeigt
    
    			analyse_loop2++;
    
    		}  // if(input[analyse_loop]=='x') {
    
    	}  // for(analyse_loop=0;analyse_loop<=sizeof(input);analyse_loop++) {
    
    }  // int full_curve_investigation_symmetry() {
    

    Code für die Variable:

    int analyse_loop;
    int analyse_loop2=0;
    int exponent_number[10];
    
    string input;
    int string_length;
    int loop;
    

    Der Inhalt von der Variable "input" wird in folgendem Code eingegeben:

    int full_curve_investigation_read_task() {
    
    	cout << "Task ";
    	cin >> input;
    
    	string_length=input.size();
    
    	if(input=="close") {
    
    		loop=8;
    
    	}  // if(input=="close") {
    
    	else {
    
    		cout << "Your Task= f(x)" << input << endl;
    		cout << "Task length: " << string_length << endl;
    
    	}  // else {
    
    	for(analyse_loop=0;analyse_loop<=string_length;analyse_loop++) {
    
    		if(input[analyse_loop]=='=') {
    
    			starting_analyse_input=analyse_loop+1;
    
    			cout << "start begin by " << starting_analyse_input << endl;
    
    			analyse_loop=110;
    
    		}  // if(input[analyse_loop]=='=') {
    
    	}  // for(analyse_loop=0;analyse_loop<=100;analyse++) {
    
    	return string_length;
    
    }  // int read_task() {
    

    Wo liegt bei der Funktion der Fehler?

    Vielen Dank im Voraus.

    MfG


  • Mod

    Wie stellst du denn fest, dass etwas nicht stimmt? Zeig uns ein vollständiges Beispiel, an dem wir den Fehler nachvollziehen können, siehe dritter Link in meiner Signatur.

    Eine Sache, die auf dem ersten Blick auffällt: Du verwendest globale Variablen. Es sind sogar alle Variablen global. Das ist ganz schlecht. Du solltest überhaupt keine einzige globale Variable haben. Warum man keine globalen Variablen verwenden sollte? Nun, der Hauptgrund ist, dass man nur ungeheurer schwer den Wert der Variablen nachvollziehen kann. Irgendeine Kleinigkeit irgendwo im Programm kann an einer ganz anderen Stelle Auswirkungen haben, die man nicht mehr nachvollziehen kann. Zum Beispiel geheimnisvolle Wertänderungen. Also genau das, was du beschreibst. Man könnte einen Zusammenhang vermuten :p .

    Du könntest also auch gleich das Programm vernünftig ohne globale Variablen schreiben, dann stellt sich der Fehler höchstwahrscheinlich von selber ab. Wenn du uns vollständigen Code zeigst, können wir dir zwar die genaue Stelle zeigen, an der es schief geht, aber die Korrektur des Fehlers wird trotzdem sein, das Programm ohne globale Variablen neu zu schreiben.



  • Moin.

    Danke für die schnelle Antwort.

    Der vollständige Code ist wie folgt:

    #include <iostream>
    
    using namespace std;
    
    // ================================================================================
    // ================================================================================
    
    string input;
    string main_input;
    
    string input_array[100];
    
    float numbers_float[20];
    
    int string_length;
    
    int loop;
    int main_loop;
    
    // ================================================================================
    // ================================================================================
    // full_curve_investigation
    // ================================================================================
    // for analyse
    
    int starting_analyse_input;
    int task_size;
    
    int analyse_loop;
    int analyse_loop2=0;
    int number_of_exponent=0;
    
    int exponent_number[10];
    
    int a_s=0;
    int p_s=0;
    
    // for analyse - ending
    // ================================================================================
    // full curve investigation - ending
    // ================================================================================
    // ================================================================================
    
    // ================================================================================
    // ================================================================================
    // ================================================================================
    
    // ================================================================================
    
    int full_curve_investigation_results() {
    
    	cout << endl;
    
    	for(analyse_loop=0;analyse_loop<=number_of_exponent;analyse_loop++) {
    
    		cout << analyse_loop+1 << " exponent = " << input_array[analyse_loop] << endl;
    
    	}  // for(analyse_loop=0;analyse_loop<next;analyse_loop++) {
    
    	cout << endl;
    }  // int full_curve_investigation_results() {
    
    // ================================================================================
    
    // ================================================================================
    
    // ================================================================================
    
    // ================================================================================
    
    // ================================================================================
    
    int full_curve_investigation_symmetry() {
    
    	cout << endl;
    	analyse_loop2=0;
    
    	for(analyse_loop=0;analyse_loop<=string_length;analyse_loop++) {
    
    		if(input[analyse_loop]=='x') {
    
    			exponent_number[analyse_loop2]=input[analyse_loop+1];
    
    			cout << exponent_number[analyse_loop2] << endl;  // hier wird ein falscher Wert angezeigt
    
    			analyse_loop2++;
    
    		}  // if(input[analyse_loop]=='x') {
    
    	}  // for(analyse_loop=0;analyse_loop<=sizeof(input);analyse_loop++) {
    
    }  // int full_curve_investigation_symmetry() {
    
    // ================================================================================
    
    int full_curve_investigation_input_divide() {
    
    	for(analyse_loop=0;analyse_loop<string_length;analyse_loop++) {  // analyse_loo
    
    		if(analyse_loop>2) {
    
    			if(input[analyse_loop]=='-') {
    
    				number_of_exponent++;
    
    			}  // if(input[analyse_loop]=="-") {
    
    			if(input[analyse_loop]=='+') {
    
    				number_of_exponent++;
    
    			}  // if(input[analyse_loop]=="+") {
    
    		}  // if(input[analyse_loop]=="+") {
    
    		input_array[number_of_exponent]=input_array[number_of_exponent] + input[analyse_loop];
    
    	}  // for(analyse_loop=0;analyse_loop<=task_size;analyse_loop++) {
    
    	// input_array[0]='\0';      -> to insert a " " in the variable (without the ")
    
    	input_array[0]="";
    
    	for(analyse_loop=1;analyse_loop<sizeof(input_array[0]);analyse_loop++) {
    
    		input_array[0]=input_array[0] + input[analyse_loop];
    
    	}  // for(analyse_loop=1;analyse_loop<sizeof(input_array[0]);analyse_loop++) {
    
    }  // int input_divide() {
    
    // ================================================================================
    
    int full_curve_investigation_task_analyse() {
    
    	full_curve_investigation_input_divide();
    
    	full_curve_investigation_symmetry();
    
    }  // int task_analyse() {
    
    // ================================================================================
    
    int full_curve_investigation_read_task() {
    
    	cout << "Task ";
    	cin >> input;
    
    	string_length=input.size();
    
    	if(input=="close") {
    
    		loop=8;
    
    	}  // if(input=="close") {
    
    	else {
    
    		cout << "Your Task= f(x)" << input << endl;
    		cout << "Task length: " << string_length << endl;
    
    	}  // else {
    
    	for(analyse_loop=0;analyse_loop<=string_length;analyse_loop++) {
    
    		if(input[analyse_loop]=='=') {
    
    			starting_analyse_input=analyse_loop+1;
    
    			cout << "start begin by " << starting_analyse_input << endl;
    
    			analyse_loop=110;
    
    		}  // if(input[analyse_loop]=='=') {
    
    	}  // for(analyse_loop=0;analyse_loop<=100;analyse++) {
    
    	return string_length;
    
    }  // int read_task() {
    
    // ================================================================================
    
    int full_curve_investigation_info() {
    
    	cout << "task must be entered in the form of the example" << endl << endl;
    	cout << "example: =4x4-3x3+2x+x1-5" << endl;
    	cout << "examplw 2: =4x4-3x3+2x+x-5" << endl << endl;
    
    }  // int info() {
    
    // ================================================================================
    
    int full_curve_investigation() {
    
    	full_curve_investigation_info();
    
    	for(loop=0;loop<=4;loop++) {
    
    		loop=0;
    
    		full_curve_investigation_read_task();
    
    		// ================================================================================
    
    		if(loop>=5) {
    
    			cout << "program is terminated" << endl;
    
    		}  // if(loop>=5) {
    
    		else {
    
    			// code for open functions
    
    			full_curve_investigation_task_analyse();
    
    			full_curve_investigation_results();
    
    		}  // else {
    
    		// ================================================================================
    
    	}  // for(loop=0;loop<=4;loop++) {
    
    }  // int full_curve_investigation() {
    
    // ================================================================================
    
    int main_menue() {
    
    	for(main_loop=0;main_loop<=4;main_loop++) {
    
    		main_loop=0;
    
    		cout << "main menue" << endl;
    		cout << endl;
    		cout << "<1> close" << endl;
    		cout << "<2> full durve investigation" << endl;
    		cout << endl;
    		cout << "> ";
    
    		cin >> main_input;
    
    		cout << endl;
    
    		if(main_input=="1") {
    
    			main_loop=7;
    
    			cout << "program is terminated" << endl;
    
    		}  // if(main_input=="1") {
    
    		if(main_input=="2") {
    
    			full_curve_investigation();
    
    		}  // if(main_input=="2") {
    
    	}  // for(main_loop=0;main_loop<=4;main_loop++) {
    
    }  // int main_menue() {
    
    // ================================================================================
    // ================================================================================
    // ================================================================================
    
    int main() {
    
    	main_menue();
    
    	return 0;
    }
    

    Der "Fehler" bzw der Wert der Variabel liegt bei dem Beispiel

    =5x5-4x4+3x2

    bei dem 1. Wert: 53
    2. Wert: 52
    3. Wert: 50

    Sicher hast du recht, das man die Variablen nicht global in das Programm schreiben soll und so das Problem auch beheben könnte.

    Danke für eure Hilfe.

    MfG



  • Du vergleichst char mit int.
    '5' == 53
    Stimmt so (ASCII vorausgesetzt).
    Es gilt:
    '5' - '0' == 5



  • Caligulaminus schrieb:

    Du vergleichst char mit int.
    '5' == 53
    Stimmt so (ASCII vorausgesetzt).
    Es gilt:
    '5' - '0' == 5

    Moin.

    Ich kann dir recht geben, das man es mit char und int anders lösen könnte. Ich sehe das Problem in Zeile 115

    exponent_number[analyse_loop2]=input[analyse_loop+1];
    

    und Zeile 117

    cout << exponent_number[analyse_loop2] << endl;  // hier wird ein falscher Wert angezeigt
    

    Nach dem ersten x müsste dann eine 53 ausgegeben werden, obwohl es in Zeile 117 ausgegeben wird. Diesen Fehler meinte ich in meinem Programm wegen den verschieden Typen.

    Auch wenn ich die Zeile 115 in folgendes umgewandelt, hatte ich keinen Erfolg:

    exponent_number[analyse_loop2]=(int)input[analyse_loop+1];
    

    (Typumwandlung ohne Erfolg)

    Danke für den Hinweis @ Caligulaminus

    MfG



  • email25251990a schrieb:

    Nach dem ersten x müsste dann eine 53 ausgegeben werden, obwohl es in Zeile 117 ausgegeben wird.

    Diesen Satz verstehe ich nicht.

    email25251990a schrieb:

    exponent_number[analyse_loop2]=(int)input[analyse_loop+1];
    

    (Typumwandlung ohne Erfolg)

    Das war nicht gemeint.

    Wenn Du aus '5' eine 5 machen willst, mußt Du '0' subtrahieren.

    exponent_number[analyse_loop2]=input[analyse_loop+1] - '0';
    


  • Auch folgende Möglichkeiten habe ich ausprobiert:

    stringstream Str;
    
    int full_curve_investigation_symmetry() {
    
    	cout << endl;
    	analyse_loop2=0;
    
    	for(analyse_loop=0;analyse_loop<=string_length;analyse_loop++) {
    
    		if(input[analyse_loop]=='x') {
    
    			Str << input[analyse_loop+1];
    
    			Str >> exponent_number[analyse_loop2];
    
    			cout << exponent_number[analyse_loop2] << endl;  // hier wird ein falscher Wert angezeigt
    
    			analyse_loop2++;
    
    		}  // if(input[analyse_loop]=='x') {
    
    	}  // for(analyse_loop=0;analyse_loop<=sizeof(input);analyse_loop++) {
    
    }  // int full_curve_investigation_symmetry() {
    


  • Caligulaminus schrieb:

    email25251990a schrieb:

    Nach dem ersten x müsste dann eine 53 ausgegeben werden, obwohl es in Zeile 117 ausgegeben wird.

    Diesen Satz verstehe ich nicht.

    email25251990a schrieb:

    exponent_number[analyse_loop2]=(int)input[analyse_loop+1];
    

    (Typumwandlung ohne Erfolg)

    Das war nicht gemeint.

    Wenn Du aus '5' eine 5 machen willst, mußt Du '0' subtrahieren.

    exponent_number[analyse_loop2]=input[analyse_loop+1] - '0';
    

    Also ich möchte in der Zeile nach dem ersten x keine 53 als Zahl ausgegeben haben sondern den eingetippten Wert.

    Jetzt dazu "exponent_number[analyse_loop2]=(int)input[analyse_loop+1];"

    Diese Zeile soll nach einem "x" (Typ String) die Zahl (Liegt alsString vor) in das Array "exponent_number" gespeichert werden.
    Die Variable "input) vom Typ string soll halt in dem Fahl in ein Int (liegt dann eine Zahl vor) abgespeichert werden.

    Sonst müsste ich dies mit einer If Abfrage alles schreiben...

    MfG



  • Dazu musst du '0' subtrahieren, wie bereits gesagt... 🙄



  • Aso ok. Nach dem Ausprobieren wurde es mir klar, danke 😉


Anmelden zum Antworten