Also wenn ich den Code angucke:
Warum eine Klasse "String" wenn in C++ "string" verfügbar wäre (besser die Klasse "Panzer" nennen).
Dann hat "String" ein Konstruktor, aber kein Destruktor.
Dann muss main entweder ein "int main()" oder ein "int main(int arc,char*argv[])" sein.
Dann kann man die Rechnung
Auch als
schreiben.
Wenn du dein Spiel noch erweitern willst (mehr Panzer) kannste auch eine std::map oder ein std::vector verwenden, um die Panzer zu speichern.
Ich formatier dir den Code mal, wie ich es machen würde...
Die Tabs wolle irgendwie nicht ins Editierfeld. Na ja...
Was mir noch aufgefallen ist: #include <string> nicht <string.h>
Du kannst die "\n" einfach wo anhängen, während am eine ein "\0" nicht notwendig ist, da es bei einer Zeichenkette automatisch angehangen wird.
Warum eine Klasse "String" wenn in C++ "string" verfügbar wäre (besser die Klasse "Panzer" nennen).
Dann hat "String" ein Konstruktor, aber kein Destruktor.
Dann muss main entweder ein "int main()" oder ein "int main(int arc,char*argv[])" sein.
Dann kann man die Rechnung
Code:
a=c-b
cout<<a*-1
Code:
a=b-c
Wenn du dein Spiel noch erweitern willst (mehr Panzer) kannste auch eine std::map oder ein std::vector verwenden, um die Panzer zu speichern.
Ich formatier dir den Code mal, wie ich es machen würde...
Code:
[SIZE=4][COLOR=#0000ff][SIZE=4][COLOR=#0000ff]#include [/COLOR][/SIZE][/COLOR][/SIZE][SIZE=4][COLOR=#a31515][SIZE=4][COLOR=#a31515]"StdAfx.h"[/COLOR][/SIZE]
[/COLOR][/SIZE][SIZE=4][COLOR=#0000ff][SIZE=4][COLOR=#0000ff]#include [/COLOR][/SIZE][/COLOR][/SIZE][SIZE=4][COLOR=#a31515][SIZE=4][COLOR=#a31515]<iostream>[/COLOR][/SIZE]
[/COLOR][/SIZE][SIZE=4][COLOR=#0000ff][SIZE=4][COLOR=#0000ff]#include [/COLOR][/SIZE][/COLOR][/SIZE][SIZE=4][COLOR=#a31515][SIZE=4][COLOR=#a31515]<string>[/COLOR][/SIZE]
[/COLOR][/SIZE][SIZE=4][COLOR=#0000ff][SIZE=4][COLOR=#0000ff]using [/COLOR][/SIZE][/COLOR][/SIZE][SIZE=4][COLOR=#0000ff][SIZE=4][COLOR=#0000ff]namespace[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=4] std;[/SIZE]
[SIZE=4][COLOR=#0000ff][SIZE=4][COLOR=#0000ff]cons t[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=4][COLOR=#0000ff][SIZE=4][COLOR=#0000ff]int[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=4] LEN = 5;[/SIZE]
[SIZE=4][COLOR=#0000ff][SIZE=4][COLOR=#0000ff]class[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=4] Panzer {[/SIZE]
[SIZE=4][COLOR=#0000ff][SIZE=4][COLOR=#0000ff]public[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=4]:[/SIZE]
[SIZE=4]string s;[/SIZE]
[SIZE=4][COLOR=#0000ff][SIZE=4][COLOR=#0000ff]int[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=4] Xp;[/SIZE]
[SIZE=4]};[/SIZE]
[SIZE=4][COLOR=#0000ff][SIZE=4][COLOR=#0000ff]int[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=4] main()[/SIZE]
[SIZE=4]{[/SIZE]
[SIZE=4]Panzer panzer[LEN];[/SIZE][SIZE=4][COLOR=#008000][SIZE=4][COLOR=#008000]//hast insgesamt 5 panzer 0.1.2.3 und 4[/COLOR][/SIZE]
[/COLOR][/SIZE][SIZE=4]Panzer zielpanzer;[/SIZE]
[SIZE=4][COLOR=#0000ff][SIZE=4][COLOR=#0000ff]int[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=4] start,ziel;[/SIZE]
[SIZE=4]panzer[0].s=[/SIZE][SIZE=4][COLOR=#a31515][SIZE=4][COLOR=#a31515]"p0"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=4]; panzer[0].Xp=0;[/SIZE]
[SIZE=4]panzer[1].s=[/SIZE][SIZE=4][COLOR=#a31515][SIZE=4][COLOR=#a31515]"p1"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=4]; panzer[1].Xp=175;[/SIZE]
[SIZE=4]panzer[2].s=[/SIZE][SIZE=4][COLOR=#a31515][SIZE=4][COLOR=#a31515]"p2"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=4]; panzer[2].Xp=1135;[/SIZE]
[SIZE=4]panzer[2].Xp=panzer[2].Xp+panzer[0].Xp-panzer[1].Xp;[/SIZE]
[SIZE=4]cout<<[/SIZE][SIZE=4][COLOR=#a31515][SIZE=4][COLOR=#a31515]"Willkommen beim XP Calculator!\n"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=4];[/SIZE]
[SIZE=4]cout<<[/SIZE][SIZE=4][COLOR=#a31515][SIZE=4][COLOR=#a31515]"Bitte geben Sie Ihren Startpanzer ein:"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=4];[/SIZE]
[SIZE=4]cin>> zielpanzer.s;[/SIZE]
[SIZE=4][COLOR=#0000ff][SIZE=4][COLOR=#0000ff]for[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=4] ([/SIZE][SIZE=4][COLOR=#0000ff][SIZE=4][COLOR=#0000ff]int[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=4] i=0;i<LEN;i++)[/SIZE][SIZE=4][COLOR=#008000][SIZE=4][COLOR=#008000]//startpanzer aussuchen[/COLOR][/SIZE]
[/COLOR][/SIZE][SIZE=4][COLOR=#0000ff][SIZE=4][COLOR=#0000ff]if[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=4](panzer[i].s==zielpanzer.s)[/SIZE]
[SIZE=4]{start=i; [/SIZE][SIZE=4][COLOR=#0000ff][SIZE=4][COLOR=#0000ff]break[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=4];}[/SIZE]
[SIZE=4]cout<<[/SIZE][SIZE=4][COLOR=#a31515][SIZE=4][COLOR=#a31515]"Bitte geben Sie ihren Zielpanzer ein:"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=4];[/SIZE]
[SIZE=4]cin>> zielpanzer.s;[/SIZE]
[SIZE=4][COLOR=#0000ff][SIZE=4][COLOR=#0000ff]for[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=4] ([/SIZE][SIZE=4][COLOR=#0000ff][SIZE=4][COLOR=#0000ff]int[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=4] i=0;i<LEN;i++)[/SIZE][SIZE=4][COLOR=#008000][SIZE=4][COLOR=#008000]//startpanzer aussuchen[/COLOR][/SIZE]
[/COLOR][/SIZE][SIZE=4][COLOR=#0000ff][SIZE=4][COLOR=#0000ff]if[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=4](panzer[i].s==zielpanzer.s)[/SIZE]
[SIZE=4]{ziel=i;[/SIZE][SIZE=4][COLOR=#0000ff][SIZE=4][COLOR=#0000ff]break[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=4];}[/SIZE]
[SIZE=4]panzer[3].Xp=panzer[start].Xp-panzer[ziel].Xp;[/SIZE]
[SIZE=4]cout<< panzer[3].Xp*(-1) << [/SIZE][SIZE=4][COLOR=#a31515][SIZE=4][COLOR=#a31515]"XP\n"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=4];[/SIZE]
[SIZE=4]cin.clear();[/SIZE]
[SIZE=4]cin.ignore(cin.rdbuf()->in_avail());[/SIZE]
[SIZE=4]cin.get(); [/SIZE]
[SIZE=4]}[/SIZE]
Was mir noch aufgefallen ist: #include <string> nicht <string.h>
Du kannst die "\n" einfach wo anhängen, während am eine ein "\0" nicht notwendig ist, da es bei einer Zeichenkette automatisch angehangen wird.
Zuletzt bearbeitet: