gero1913 Temat założony przez niniejszego użytkownika |
Program zwraca wartosc -1 przy wprowadzeniu do niego danych » 2014-10-20 16:30:49 Witam.Jestem początkujący także proszę śmiało krytykować konstruktywnie mój kod i wszystko co w nim złe,chciałbym się jak najwięcej nauczyć. Mój problem jest następujący: napisałem program w c++ - działał Przepisałem go na C i przelozylem na jezyk angielski i wywala mi błąd gdy tylko podam wartość pierwszej zmiennej. Siedziałem nad tym godzinę i nie mogę ogarnąć za cholerę także postanowiłem szukać pomocy tutaj. Program ten to taka prymitywna konsolowa gierka oparta głównie na losowaniu liczb. ( kupujesz/sprzedajesz akcje i starasz sie zgromadzic jak najwiecej pieniedzy) Tak jak już prosiłem: Jeżeli cokolwiek jest złego w tym kodzie i chce się wam go skrytykować to byłbym bardzo wdzięczny. #include <stdio.h> #include <stdlib.h> #include <time.h>
int main() { srand( time( NULL ) ); int Value_Of_Assets = 0, Number_Of_Rounds, Choice, Number_Of_Shares, Cash = 100000; int round = 1, Value_Of_Share_IT, Value_Of_Share_Media, Value_Of_Share_Finances, Value_Of_Share_Infrastructure, Value_Of_Share_Energetics; int Numer_Of_Shares_IT, Number_Of_Shares_Media, Number_Of_Shares_Finances, Number_Of_Shares_Infrastructure, Number_Of_Shares_Energetics; Numer_Of_Shares_IT = Number_Of_Shares_Media = Number_Of_Shares_Finances = Number_Of_Shares_Infrastructure = Number_Of_Shares_Energetics = 0; Value_Of_Share_IT = Value_Of_Share_Media = Value_Of_Share_Finances = Value_Of_Share_Infrastructure = Value_Of_Share_Energetics = 100; printf( "How many rounds should the game last for?\n" ); scanf( "%d", Number_Of_Rounds ); for( round = 1; round <= Number_Of_Rounds; round++ ) { printf( "Current values of shares in different sectors of economy: \n" ); printf( "Value of shares in financial sector is: %d $ per share\n", Value_Of_Share_Finances ); printf( "Value of shares in IT sector is: %d $ per share\n", Value_Of_Share_IT ); printf( "Value of shares in energetic sector is: %d $ per share\n", Value_Of_Share_Energetics ); printf( "Value of shares in media sector is: %d $ per share\n", Value_Of_Share_Media ); printf( "Value of shares in infrastructure sector is: %d $ per share\n", Value_Of_Share_Infrastructure ); printf( "Now you are able to invest your capital and capitalize your assets \n" ); printf( "\n\n\n" ); printf( "Round : %d\n", round ); printf( "Cash you currently have: %d $\n", Cash ); printf( "1.Financial Sector (you have: %d shares,each is worth: %d$\n)", Number_Of_Shares_Finances, Value_Of_Share_Finances ); printf( "2.IT Sector (you have: %d shares,each is worth: %d $ \n)", Numer_Of_Shares_IT, Value_Of_Share_IT ); printf( "3.Energetical Sector (you have: %d shares,each is worth: %d $ \n)", Number_Of_Shares_Energetics, Value_Of_Share_Energetics ); printf( "4.Media Sector (you have: %d shares,each is worth: %d $ \n)", Number_Of_Shares_Media, Value_Of_Share_Media ); printf( "5.Infrastructure Sector (you have: %d shares,each is worth: %d $\n)", Number_Of_Shares_Infrastructure, Value_Of_Share_Infrastructure ); printf( "6.Finish current round\n" ); scanf( "%d", Choice ); do { switch( Choice ) { case 1: { printf( "Would you like to buy or sell?\n" ); printf( "1.I'd like to buy\n" ); printf( "2.I'd like to sell\n" ); scanf( "%d", Choice ); if( Choice == 1 ) { printf( "How many shares of Financial Sector would you like to buy? (maximum is: %d\n)",( Cash / Value_Of_Share_Finances ) ); scanf( "%d", Number_Of_Shares ); if( Number_Of_Shares >( Cash / Value_Of_Share_Finances ) ) { printf( "You can't afford this amount of shares!\n" ); continue; } Cash = Cash - Number_Of_Shares * Value_Of_Share_Finances; Number_Of_Shares_Finances = Number_Of_Shares_Finances + Number_Of_Shares; break; } else { printf( "How many shares of Financial Sector would you like to sell? \n" ); scanf( "%d", Number_Of_Shares ); } if( Number_Of_Shares > Number_Of_Shares_Finances ) { continue; } Cash = Cash + Number_Of_Shares * Value_Of_Share_Finances; Number_Of_Shares_Finances = Number_Of_Shares_Finances - Number_Of_Shares; break; } case 2: { printf( "Would you like to buy or sell?\n" ); printf( "1.I'd like to buy\n" ); printf( "2.I'd like to sell\n" ); scanf( "%d", Choice ); if( Choice == 1 ) { printf( "How many shares of IT Sector would you like to buy? (maximum is: %d\n)",( Cash / Value_Of_Share_IT ) ); scanf( "%d", Number_Of_Shares ); if( Number_Of_Shares >( Cash / Value_Of_Share_IT ) ) { printf( "You can't afford this amount of shares!\n" ); continue; } Cash = Cash - Number_Of_Shares * Value_Of_Share_IT; Numer_Of_Shares_IT = Numer_Of_Shares_IT + Number_Of_Shares; break; } else { printf( "How many shares of IT Sector would you like to sell?\n" ); scanf( "%d", Number_Of_Shares ); } if( Number_Of_Shares > Numer_Of_Shares_IT ) { continue; } Cash = Cash + Number_Of_Shares * Value_Of_Share_IT; Numer_Of_Shares_IT = Numer_Of_Shares_IT - Number_Of_Shares; break; } case 3: { printf( "Would you like to buy or sell?\n" ); printf( "1.I'd like to buy\n" ); printf( "2.I'd like to sell\n" ); scanf( "%d", Choice ); if( Choice == 1 ) { printf( "How many shares of Energetical Sector would you like to buy? (maximum is: %d\n)",( Cash / Value_Of_Share_Energetics ) ); scanf( "%d", Number_Of_Shares ); if( Number_Of_Shares >( Cash / Value_Of_Share_Energetics ) ) { printf( "You can't afford this amount of shares!\n" ); continue; } Cash = Cash - Number_Of_Shares * Value_Of_Share_Energetics; Number_Of_Shares_Energetics = Number_Of_Shares_Energetics + Number_Of_Shares; break; } else { printf( "How many shares of Energetical Sector would you like to sell?\n" ); scanf( "%d", Number_Of_Shares ); } if( Number_Of_Shares > Number_Of_Shares_Energetics ) { continue; } Cash = Cash + Number_Of_Shares * Value_Of_Share_Energetics; Number_Of_Shares_Energetics = Number_Of_Shares_Energetics - Number_Of_Shares; break; } case 4: { printf( "Would you like to buy or sell?\n" ); printf( "1.I'd like to buy\n" ); printf( "2.I'd like to sell\n" ); scanf( "%d", Choice ); if( Choice == 1 ) { printf( "How many shares of Media Sector would you like to buy? (maximum is: %d\n)",( Cash / Value_Of_Share_Media ) ); scanf( "%d", Number_Of_Shares ); if( Number_Of_Shares >( Cash / Value_Of_Share_Media ) ) { printf( "You can't afford this amount of shares!\n" ); continue; } Cash = Cash - Number_Of_Shares * Value_Of_Share_Media; Number_Of_Shares_Media = Number_Of_Shares_Media + Number_Of_Shares; break; } else { printf( "How many shares of Media Sector would you like to sell?" ); scanf( "%d", Number_Of_Shares ); } if( Number_Of_Shares > Number_Of_Shares_Media ) { continue; } Cash = Cash + Number_Of_Shares * Value_Of_Share_Media; Number_Of_Shares_Media = Number_Of_Shares_Media - Number_Of_Shares; break; } case 5: { printf( "Would you like to buy or sell?\n" ); printf( "1.I'd like to buy\n" ); printf( "2.I'd like to sell\n" ); scanf( "%d", Choice ); if( Choice == 1 ) { printf( "How many shares of Infrastructure Sector would you like to buy? (maximum is: %d\n)",( Cash / Value_Of_Share_Infrastructure ) ); scanf( "%d", Number_Of_Shares ); if( Number_Of_Shares >( Cash / Value_Of_Share_Infrastructure ) ) { printf( "You can't afford this amount of shares!\n" ); continue; } Cash = Cash - Number_Of_Shares * Value_Of_Share_Infrastructure; Number_Of_Shares_Infrastructure = Number_Of_Shares_Infrastructure + Number_Of_Shares; break; } else { printf( "How many shares of Infrastructure Sector would you like to sell?" ); scanf( "%d", Number_Of_Shares ); } if( Number_Of_Shares > Number_Of_Shares_Infrastructure ) { continue; } Cash = Cash + Number_Of_Shares * Value_Of_Share_Infrastructure; Number_Of_Shares_Infrastructure = Number_Of_Shares_Infrastructure - Number_Of_Shares; break; } case 6: { Value_Of_Share_Energetics = Value_Of_Share_Energetics +(( rand() % 20 ) +( - 10 ) ); Value_Of_Share_Finances = Value_Of_Share_Finances +(( rand() % 30 ) +( - 14 ) ); Value_Of_Share_Infrastructure = Value_Of_Share_Infrastructure +(( rand() % 33 ) +( - 15 ) ); Value_Of_Share_IT = Value_Of_Share_IT +(( rand() % 15 ) +( - 4 ) ); Value_Of_Share_Media = Value_Of_Share_Media +(( rand() % 11 ) +( - 4 ) ); system( "clear" ); break; } default: { printf( "There is no such option\n" ); break; } } } while(( Choice != 6 ) &&( round < Number_Of_Rounds ) ); } Value_Of_Assets = Cash + Number_Of_Shares_Media * Value_Of_Share_Media + Numer_Of_Shares_IT * Value_Of_Share_IT + Number_Of_Shares_Energetics * Value_Of_Share_Energetics + Number_Of_Shares_Finances * Value_Of_Share_Finances + Number_Of_Shares_Infrastructure * Value_Of_Share_Infrastructure; if( round == Number_Of_Rounds ) { printf( "GAME OVER\n" ); if( Value_Of_Assets >= 100000 ) { printf( "VALUE OF YOUR ASSETS: %d\n", Value_Of_Assets ); printf( "CONGRATULATIONS,YOU HAVE EARNED: %d$\n",( Value_Of_Assets - 100000 ) ); } else { printf( "VALUE OF YOUR ASSETS: %d$\n", Value_Of_Assets ); printf( "YOU HAVE LOST YOUR TIME AND %d$\n ",( 100000 - Value_Of_Assets ) ); } } return 0; } |