array inhalt in Zahl umwandeln
-
Hi, ich versuche ein kleines Programm zu schreiben hab aber ein kleines Problem.
Ich will
resultat[i]
in eine Integer Zahl umschreiben.
for(int i=0; i<ra-1; i++) { resultat[i]=rand()%max+1; } int resultat1=resultat[i]; printf("%d sagen Ja\n",resultat1);
-
Huhu,
hier bitte:
itoa == integer to ascii
atoi == ascii to integerHoffe habe Dich nicht falsch verstanden. Wenn "umschreiben" das Äquivalent zu konvertieren ist, dann passt es.
-
Wie ist denn resultat[] definiert?
-
shevazri schrieb:
hab aber ein kleines Problem.
Welches denn?
Egal, welchen Typ
resultat[]
hat,rand()
ist einint
,max
muss eine Ganzzahl sein, also genauso gut wie einint
, daher istrand()%max+1
eine Ganzzahl, und die kann man scheinbar problemlos anresultat[n]
zuweisen. Wenn man mögliche Überläufe nicht berücksichtigt, kann das also gar nicht schiefgehen.sunny31 schrieb:
itoa == integer to ascii
Was ist denn das?
-
µngbd schrieb:
sunny31 schrieb:
itoa == integer to ascii
Was ist denn das?
Verstehe deine Frage nicht
-
sunny31 schrieb:
Verstehe deine Frage nicht
Nur der übliche ANSI-Wahnsinn. Steht halt bei C99 nicht im Index (und auch sonst nirgends). Das Ding gibt es einfach nicht.
-
Huhu,
ohhhh. Hab´s gerade gelesen:
This function is not defined in ANSI-C and is not part of C++, but is supported by some compilers.
Muss dir wieder danken,... Danke! :xmas1:
-
Hey
Ich habe versucht nach euren Tipps umzubauen und hab es auf einen Fehler runter geschaftt.Nur noch in Zeile 6 hat es noch einen Fehler:
error: cannot convert 'int*' to 'const char* for argument '1' to 'int atoi(const char*)
for(int i=0; i<ra; i++) { resultat[i]=rand()%max+1; } int resultat_ende=atoi(resultat); printf("%d sagen Ja\n",resultat_ende); int neinp=100-resultat_ende; int stimmen=wahlbeteiligung/100;
-
Hallo,
resultat ist doch ein Array von ints! Da benötigst du doch keine Umwandlung von Text in einen numerischen Typ. Also noch mal:
Wozu die einzelnen Werte:
resultat[0]
resultat[1]
resultat[2]
...
...
...
resultat[ra - 1]?
Wenn nicht der letzte Wert:
resultat[ra - 1]
in
resultat_ende
landen soll
(und das soll er scheinbar nicht, wenn:
int i = 0; for(i=0; i<ra-1; i++) { resultat[i]=rand()%max+1; } int resultat1=resultat[i - 1];// (nicht i, das wäre eine Position zu viel) printf("%d sagen Ja\n",resultat1);
nicht das tut, was es soll)
was soll dann in resultat_ende stehen?
MfG,
Probe-Nutzer