Navigacija
Lista poslednjih: 16, 32, 64, 128 poruka.

Problem sa kodom

[es] :: C/C++ programiranje :: C/C++ za početnike :: Problem sa kodom

[ Pregleda: 2280 | Odgovora: 3 ] > FB > Twit

Postavi temu Odgovori

Autor

Pretraga teme: Traži
Markiranje Štampanje RSS

s@le
Apatin

Član broj: 68410
Poruke: 11
79.101.179.*



Profil

icon Problem sa kodom15.01.2008. u 20:11 - pre 198 meseci
Pozdrav svima.
Totalni sam pocetnik u svetu programiranja. Poceo sam da ucim c++ iz knjige "Learn to program C++" i sve je bilo ok dok nisam naisao na deo koda koji kod mene ne radi, a po knjizi bi trebao da radi. Pretpostavljam da je neki banalan razlog i zamolio bih nekog da mi objasni sta nije u redu.

Problem je u tome sto nakon pokretanja programa i upisa "yes", umesto mogucnosti da odaberem jednu od tri opcije, program prestaje da radi.

Code:

#include <iostream>
#include <string>

using namespace std;

int main()

{
    char response[256];
    string moreBankingBusiness;

    cout << "Do you want to do some banking? ";
    cin >> moreBankingBusiness;

    for (int i = 0; i < moreBankingBusiness.length(); i++)
    {
        moreBankingBusiness[i] = toupper (moreBankingBusiness[i]);
    }

    while (moreBankingBusiness == "YES")
    {
        cout << "What would you like to do? " << "(1=Deposit, 2=Withdraw, 3=Get Balance): ";

        cin.getline(response,256);

        

        
            if (atoi(response) < 1) | (atoi(response) > 3)
            {
                cout << response << endl << " - is not a valid banking function " << endl << endl;
                return 1;
            }

            else 
                if (atoi(response) == 1)
            {
                cout << "You pressed 1";
            }

            if (atoi(response) == 2)
            {
                cout << "You pressed 2";
            }

            if (atoi(response) == 3)
            {
                cout << "You pressed 3";
            }

    }
        
    cout << endl << endl << "Thanks for banking with us! ";

    return 0;
}
 
Odgovor na temu

laki_srt
laki_srt
ucenik

Član broj: 166987
Poruke: 183
*.tippnet.co.yu.



Profil

icon Re: Problem sa kodom15.01.2008. u 21:09 - pre 198 meseci
Evo ti kod koji radi pa ti prostudiraj gde si pogresio:
Code:

#include <iostream>
#include <string>

using namespace std;

int main()

{
    char response[256];
    string moreBankingBusiness;

    cout << "Do you want to do some banking? ";
    cin >> moreBankingBusiness;

    for (int i = 0; i < moreBankingBusiness.length(); i++)
    {
        moreBankingBusiness[i] = toupper (moreBankingBusiness[i]);
    }

    while (moreBankingBusiness == "YES")
    {
        cout << "What would you like to do? " << "(1=Deposit, 2=Withdraw, 3=Get Balance,4=Exit): ";

        cin>>response;

        

        
        if ((atoi(response) < 1) | (atoi(response) > 4))
            {
                cout << response<<endl << " - is not a valid banking function " << endl << endl;
                return 1;
            }

        else {
                if (atoi(response) == 1)
            {
                cout << "You pressed 1"<<endl;
            }

            if (atoi(response) == 2)
            {
                cout << "You pressed 2"<<endl;
            }

            if (atoi(response) == 3)
            {
                cout << "You pressed 3"<<endl;
            }
             if (atoi(response) == 4)
            {
              cout << endl << endl << "Thanks for banking with us! ";
              break;
            }

        }    }
        
    

    return 0;
}
 
Odgovor na temu

s@le
Apatin

Član broj: 68410
Poruke: 11
79.101.179.*



Profil

icon Re: Problem sa kodom15.01.2008. u 23:30 - pre 198 meseci
Sretene, hvala na odgovoru. Moram priznati da je moja greska sto nisam odmah postovao ceo kod, vec sam pokusao da izdvojim i modifikujem deo koji ne radi.

Ovo je originalan kod programa iz knjige:
Code:

#include <iostream>
#include <string>

using namespace std;

int main()
{
    float balance = 0;
    float newBalance = 0;
    float adjustment = 0;
    char response[256];
    string moreBankingBusiness;
    cout << "Do you want to do some banking? ";
    cin >> moreBankingBusiness;
    
    for (int i = 0; i < moreBankingBusiness.length(); i++) {
            moreBankingBusiness[i] = toupper (moreBankingBusiness[i]);
            }

        while (moreBankingBusiness == "YES") {

            cout << "What would you like to do? " << "(1=Deposit, 2=Withdraw, 3=Get Balance): ";
            cin.getline(response,256);
        
            if (strlen(response) == 0) {
                cout << "You must make a selection";
                return 1;
                }
            else
                if (atoi(response) < 1 | atoi(response) > 3) {
                cout << response << " - is not a valid banking function";
                return 1;
                }

            if (atoi(response) == 1) {
                cout << "Enter the Deposit Amount: ";
                cin >> adjustment;
                newBalance = balance + adjustment;
            
                cout << endl << endl << "*** SMILEY NATIONAL BANK ***" << endl << endl;
                cout << "Old Balance is: " << balance << endl;
                cout << "Adjustment is: +" << adjustment << endl;
                cout << "New Balance is: " << newBalance << endl <<endl;
            }

            if (atoi(response) == 2) {
                cout << "Enter the Withdrawal Amount: ";
                cin >> adjustment;
                newBalance = balance - adjustment;

                cout << endl << endl << "*** SMILEY NATIONAL BANK ***" << endl << endl;
                cout << "Old Balance is: " << balance << endl;
                cout << "Adjustment is: -" << adjustment << endl;
                cout << "New Balance is: " << newBalance << endl <<endl;
            }

            if (atoi(response) == 3) {
                cout << endl << endl << "*** SMILEY NATIONAL BANK ***" << endl << endl;
                cout << "Your current Balance is: " << newBalance << endl <<endl;
            }
                balance = newBalance;
                cout << "Do you have more banking business? ";
                cin >> moreBankingBusiness;
                
                for (int i = 0; i < moreBankingBusiness.length(); i++) {
                    moreBankingBusiness[i] = toupper (moreBankingBusiness[i]);
                }
            } 
            
        cout << endl << endl << "Thanks for banking with us!";
        return 0;
}


Ko bude pokrenuo kod videce da program ne radi kako treba. Najvise me buni sto je sve isto kao u knjizi, samo sto kod mene ne radi.
Mislim da sam pronasao u cemu je greska. Potrebno je dodati cin.ignore() pre cin.getline() i sve izgleda da radi kako treba. E sad samo jos ako neko moze da mi objasni zasto se ovo desava? Mislim da nije do stamparske greske jer ima vise primera koda gde se ovo desava.

Pozdrav, Sasa

 
Odgovor na temu

laki_srt
laki_srt
ucenik

Član broj: 166987
Poruke: 183
*.tippnet.co.yu.



Profil

icon Re: Problem sa kodom16.01.2008. u 00:42 - pre 198 meseci
Mislim da je bolje koristiti samo cin>>response; mesto cin.getline(response.256); inace getline(a,b);ulazni red iz promenljive a ucitava se u promenljivu b,tako da tu nema puno logike posto on treba samo da ucita jedan broj a ne celu liniju niski. Greska se verovatno ponavlja zato sto je onaj ko je pisao knjigu kad je jednom napisao kod verovatno ga nije pregledao pa je svugde gde se koristi slican kod kopirao.
Pozdrav!
 
Odgovor na temu

[es] :: C/C++ programiranje :: C/C++ za početnike :: Problem sa kodom

[ Pregleda: 2280 | Odgovora: 3 ] > FB > Twit

Postavi temu Odgovori

Navigacija
Lista poslednjih: 16, 32, 64, 128 poruka.