Buckys C++ Programming Tutorials – 70 – Reviewing the Final Program

[ad_1]
Facebook –
GitHub –
Google+ –
LinkedIn –
reddit –
Support –
thenewboston –
Twitter –


Posted

in

by

Tags:

Comments

34 responses to “Buckys C++ Programming Tutorials – 70 – Reviewing the Final Program”

  1. snl proof ffev4 Avatar

    here saved way more lines

    #include <iostream>
    #include <string>
    #include <fstream>

    int getf () {
    int ausw;

    std::cout << "1. Plain Itemsn2. Helpful itemsn3. Itemsn4. Quit Programn";
    std::cin >> ausw;
    return ausw;
    }

    void showf(int ausw) {
    ifstream objectFile("object.txt")
    string name;
    double val;

    switch(ausw) {
    case 1: //Plain items
    while (objectFile >> name >> val) {
    if (val == 0) {
    std::cout << "Name: " << name << ' Power: No Power' << std::endl;
    } }
    break;
    case 2: //Positive items
    while (objectFile >> name >> val) {
    if (val > 0) {
    std::cout << "Name: " << name << ' Power:' << val << std::endl;
    } }
    break;
    case 3: //Negative items
    while (objectFile >> name >> val) {
    if (val < 0) {
    std::cout << "Name: " << name << ' Power: ' << val << std::endl;
    } }
    break;
    }
    }

    int main() {
    int ausw;

    ausw = getf();

    while (ausw != 4) {
    while (ausw > 0 and ausw < 4)
    showf(ausw);
    }
    }

  2. Jayanth Juturi Avatar

    #include <iostream>
    #include<fstream>
    using namespace std;
    int getwhattheywant();
    void displayitem(int x);
    //main function
    int main()
    {
    int whattheywant;
    whattheywant=getwhattheywant();

    while(whattheywant!=4)
    {
    switch(whattheywant)
    {
    case 1:
    displayitem(1);
    break;
    case 2:
    displayitem(2);
    break;
    case 3:
    displayitem(3);
    break;
    }
    whattheywant=getwhattheywant();
    }
    }
    int getwhattheywant(){
    int choice;
    cout<<"1-justplain"<<endl;
    cout<<"2-helpfuliten"<<endl;
    cout<<"3-harmfulitem"<<endl;
    cout<<"4-quit"<<endl;
    cin>>choice;
    return choice;

    }

    void displayitem(int x)
    {
    ifstream objectFile("jukebox.txt");
    string name;
    double power;
    if(x==1)
    {
    while(objectFile>>name>>power)
    {
    if(power==0)
    {
    cout<<name<<":"<<power<<endl;
    }
    }
    }
    if(x==2)
    {
    while(objectFile>>name>>power)
    {
    if(power>0)
    {
    cout<<name<<":"<<power<<endl;
    }
    }
    }
    if(x==3)
    {
    while(objectFile>>name>>power)
    {
    if(power<0)
    {
    cout<<name<<":"<<power<<endl;
    }
    }
    }

    }

  3. Raf Avatar

    The items with negative values aren't printing :<

  4. usman rafique Avatar

    which compiler u r using

  5. Umer Iqbal Avatar

    hey Bucky how to delet a particular record that we want from this file?

  6. first last Avatar

    Thanks Bucky,you're amazing,
    Here's what I did:-
    //Check this directory for readme.txt
    #include<fstream>
    #include<iostream>
    #include<iomanip>
    using namespace std;
    //main function();
    int getchoice();
    void displayitems(int n);
    int main(){
    int choice;
    choice=getchoice();
    while(choice!=4){
    if(choice>4){cout<<"enter from 1 to 4"<<endl;}
    switch(choice){
    case 1:
    displayitems(1);
    break;
    case 2:
    displayitems(2);
    break;
    case 3:
    displayitems(3);
    break;
    }
    choice=getchoice();
    }
    }
    //getchoice() function
    int getchoice(){
    int option;
    cout<<"1— just plain items"<<endl;
    cout<<"2—helpful items"<<endl;
    cout<<"3—harmful items"<<endl;
    cout<<"4—quit"<<endl;
    cin>>option;
    return option;
    }
    //displayitems();
    void displayitems(int x)
    {
    ifstream objfile("game.txt");
    string obj;
    double points;
    while(objfile>>obj>>points){
    if((x==1)&&(points==0)){cout<<obj<<setw(10)<<points<<endl;}
    if((x==2)&&(points>0)){cout<<obj<<setw(10)<<points<<endl;}
    if((x==3)&&(points<0)){cout<<obj<<setw(10)<<points<<endl;}
    }
    }

  7. steve frt Avatar

    Thanks you so much

  8. Captain Angry Avatar

    soda is helpful item? really?

  9. kegan Avatar

    I'll take the meth and dirtyneedles over bucky's "teaching" any day. Way less harmful.

  10. Yu Zhao Avatar

    So where is the entropy of this tutorial given the previous two?

  11. Pizza Guy Avatar

    My Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    using namespace std;

    // global variable : name;
    string name;
    int getWhatTheyWant ();
    void displaysItem (int x);

    // main function
    int main(){

    cout << "Enter your character name :" << endl;
    cin >> name;
    int whatTheyWant;
    whatTheyWant = getWhatTheyWant();
    while(whatTheyWant != 5){
    displaysItem(whatTheyWant);
    whatTheyWant = getWhatTheyWant();
    }

    }
    // getWhatTheyWant function
    int getWhatTheyWant ()
    {
    int input;
    cout << "" << endl;
    cout << "1 —- Displays items with power 0" << endl;
    cout << "2 —- Displays items with power > 0" << endl;
    cout << "3 —- Displays items with power < 0" << endl;
    cout << "4 —- Displays the whole list of items which will affect " << name << endl;
    cout << "5 —- Quit" << endl;
    cout << "" << endl;
    cin >> input;
    return input;
    }

    // displaysItem function
    void displaysItem (int x)
    {
    string id;
    double power;
    ifstream myFile("random.txt");

    while(myFile >> id >> power){
    if (x==1 && power == 0){
    cout << id << " " << power << endl;
    }
    if (x==2 && power > 0){
    cout << id << " " << power << endl;
    }
    if(x == 3 && power < 0){
    cout << id << " " << power << endl;
    }
    if(x==4){
    cout << id << " " << power << endl;
    }

    }
    myFile.close();
    }

  12. pranay gottipamula Avatar

    hi bucky, can i write object.txt file in this program itself instead of writing it separately, i tried it but it stuck up in a loop, can u tell me how to do it??

  13. Jess Huang Avatar

    You do not need the switch statement in the while loop. You could just pass in the user input right into the displayItems function right away.
    Heres How:
    while(user_input != 4){ //main loop to control whether the program stays on or turns off
    displayItems(user_input); // all that is needed to pass in the user input into the function
    if(user_input != 1 && user_input != 2 && user_input != 3){
    cout << "Your input is invalid! Please enter a number between 1 and 4!" << endl;
    }
    user_input = getNum();
    }

  14. Sara Tokic Avatar

    I have problem ,I wrote the exact same code and it occurs that it doesn't even read from the file. I just get to enter the choice and I get the Menu constantly.

  15. Jose Mascarua Avatar

    Why in main is written twice, at the beginning and at the end, "whatTheyWant=getWhatTheyWant ();"?

  16. Pete the Paper Boat Avatar

    This is a little more effective (Change variable names)
    int main()
    {
    int uInput;
    uInput = objectOut();

    while (uInput != 4)
    {
    system("cls");
    displayItems(uInput);
    uInput = objectOut();
    }
    }

  17. JUBIN DAVID Avatar

    thank you!! can you please explain how to modify the power values??

  18. Grace Tio Avatar

    Could you make a video where you write to the file and read from it in the same program please?

  19. ziad hassan Avatar

    Guys please tell me why i write the code as same as bucky did, but i come with different results?!!! :

  20. sabri ömür yıldırmaz Avatar

    Hello!
    At the display part, my code stucks in infinite while loop. Can you help me?

    ifstream objj("game.txt");
    string Name;
    double value;

    if (x == 1) {
    while (objj >> Name >> value) {
    if (value > 0) {
    //cout << "Useful Items= n" << endl;
    cout << Name << ' ' << value << endl;

    }
    }
    ……………..

  21. Sofia Lopez Avatar

    Thank you!!!! I had a 3 hours class today and didn´t understand a single thing, I just watched your videos and is crystal clear! 😀

  22. Dung Ngo Avatar

    i messed up something but i dont know how to fix it ! i coeded excatly what is showed in the videos but the while loop kept running again not get to the end point ? how can i fix it ?

  23. Payam Khorramhahi Avatar

    I wrote the exact program but when I enter the number the names are displayed but the loop makes it just going. what change should I make in the program so that it only loop through the file once ?

  24. Tommy Ip Avatar

    Should you prototype a function in a production software or put the function before the main function? The name 'prototype' gives me an impression that it is a temporary thing.

  25. Aaron Day Avatar

    Switch statement seemed a bit excessive; how about this?

    int main () {
    int whatTheyWant;
    do {
    whatTheyWant = getWhatTheyWant();
    displayItems(whatTheyWant);
    } while(whatTheyWant != 4);
    }

  26. Renato Lopes Avatar

    You feel so happy on this tutorial…? I mean I onl felt you hapiness like 2 times on your tutorials (not counting the video with muahmuahm)

  27. Abdulwahab Abutaleb Avatar

    I wrote the program without any function and it works perfectly well

    #include <iostream>
    #include<fstream>
    using namespace std;

    // Main Function
    int main()
    {
    int RequiredItem;
    string Name;
    double Power;
    do{

    cout<< "n1 – Plain Items" << endl;
    cout<< "2 – Helpful Items" << endl;
    cout<< "3 – Harmful Items" << endl;
    cout<< "4 – Quit n" << endl;
    cin >> RequiredItem;

    ifstream ObjectsFile ("Objects.txt");
    switch(RequiredItem){
    case 1:
    while(ObjectsFile>> Name >> Power){
    if (Power==0){
    cout<< Name<< " " << Power << endl;
    }
    }
    break;
    case 2:
    while(ObjectsFile>> Name >> Power){
    if (Power>0){
    cout<< Name<< " " << Power << endl;
    }
    }
    break;
    case 3:
    while(ObjectsFile>> Name >> Power){
    if (Power<0){
    cout<< Name<< " " << Power << endl;
    }
    }

    break;

    }

    }while(RequiredItem !=4);
    }

  28. Tarique Baig Avatar

    which compiler is it ? notepad ++ ?? how to run c,c++ programs ?

  29. S1CK Avatar

    Bucky, congrats on the 1 million subscribers! 🙂

  30. Benny Boom Avatar

    here is what i did:
    #include <iostream>
    #include <string>
    #include <fstream>

    using namespace std;

    int getWhatTheyWant();
    void displayItem(int x);
    //main function
    int main(){

    int whatTheyWant;
    whatTheyWant = getWhatTheyWant();

     while(whatTheyWant !=4){
     displayItem(whatTheyWant);
     whatTheyWant = getWhatTheyWant();
     }
    }
    //getWhatTheyWant function
    int getWhatTheyWant(){
     int choice;
       cout<<"n1 — just plain items" <<endl;
       cout<<"2 — Helpfull items" <<endl;
       cout<<"3 — Harmfull items" <<endl;
       cout<<"4 — Quit programn" <<endl;

       cin >>choice;
       return choice;
    }

    //display items function
    void displayItem(int x){

        ifstream objFile("text.txt");

        string name;
        double power;

       while(objFile >>name >>power){
        if(x==1 && power == 0){
            cout<<name<< ' ' <<power <<endl;
            }
            else if(x==2 && power>0){
                cout<<name<< ' ' <<power<<endl;
            }
            else if(x==3 && power < 0){
                cout<<name<< ' ' <<power<<endl;
            }
        }

    }

  31. Arlon Bernard Avatar

    mine wouldn't run without the string header.
    don't know why his worked, but great tutorial non the less.

  32. Lord Lefan Avatar

    i get 82240 when i clicked 1, i get "8224" in between the name and power, what the heck?

  33. Haikun Wang Avatar

    How to modify the code if i want to write the file when reading it? For example, if the whattheywant is 3 then change all the negative numbers into -1 ; if the what they want is 2 then change all the positive numbers into 1.  Can we write and read at the same time?

  34. Nubro Zaref Avatar

    why did he include the switch statement when he could just call displayItems(whatTheyWant);

Leave a Reply

Your email address will not be published. Required fields are marked *