You are viewing our Forum Archives. To view or take place in current topics click here.
Question About my Code [C++]
Posted:

Question About my Code [C++]Posted:

Taylor
  • Summer 2018
Status: Offline
Joined: Apr 30, 201410Year Member
Posts: 5,962
Reputation Power: 15122
Status: Offline
Joined: Apr 30, 201410Year Member
Posts: 5,962
Reputation Power: 15122
Before I say anything, I am new to coding and programming, so. Take it easy on me

I have a project for my class, and I have finished the code, or at least I think I have.

Here is my code, my question will follow.

//Taylor Lynch
//Messages
//Print 3 Messages
//Period 5

#include <iostream.h>
#include <conio.h>
#include <dos.h>
#include <stdio.h>

main()
{
    float x;
    textcolor(WHITE);
    cprintf(Enter either 1, 2 or 3: );
    cin>>x;
    if(x>3)
    {
    cout<<Number is not valid, Please try again;
    getch();
    clrscr();
    cprintf(New Number: );
    cin>>x;
    }
    if(x<1)
    {
    cout<<Number is not valid, Please try again;
    getch();
    clrscr();
    cprintf(New Number: );
    cin>>x;
    }
    if(x==1)
    {
    cout<<What is a teacher without students?;
    delay(100);
    cout<< Happy.
    }
    if(x==2)
    {
    cout<<Student: Would you punish me for something I didnt do?\n;
    delay(200);
    cout<<Teacher: Of course not.\n;
    delay(200);
    cout<<Student: Good, because I didnt do my C++ assignment.\n;
    }
    if(x==3)
    cout<<Teacher: Where is your textbook?\n;
    delay(200);
    cout<<Student: Its at my house.\n;
    delay(200);
    cout<<Teacher: Well, what is it doing there?\n;
    delay(200);
    cout<<Student: Having a better day than me.\n;
    getch();
return 0;
}


Want I want to know if, at the beginning, do I have to use 'int x;' or will it work the same with 'float x;'.

What's the difference between the two?
#2. Posted:
ObscureCoder
  • Resident Elite
Status: Offline
Joined: Jun 29, 201311Year Member
Posts: 211
Reputation Power: 13
Status: Offline
Joined: Jun 29, 201311Year Member
Posts: 211
Reputation Power: 13
Floats using floating point representation to allow you to store decimal values. Integers are designed to store whole numbers - which is what you're doing and should be using.

float x = 34.2f;
int y = 34;

For your program, you'd be reading the input from the standard cin and then using a switch statement to match to an enum int value. This removes all the if-else-if mess and improves readability and understanding of your code.
#3. Posted:
ObscureCoder
  • Resident Elite
Status: Offline
Joined: Jun 29, 201311Year Member
Posts: 211
Reputation Power: 13
Status: Offline
Joined: Jun 29, 201311Year Member
Posts: 211
Reputation Power: 13
Also, from actually looking at your code I can tell you it's invalid.
#4. Posted:
Taylor
  • E3 2017
Status: Offline
Joined: Apr 30, 201410Year Member
Posts: 5,962
Reputation Power: 15122
Status: Offline
Joined: Apr 30, 201410Year Member
Posts: 5,962
Reputation Power: 15122
Thanks for the help, I'll have to have another look at it.

This whole programming thing is just a big headache.
#5. Posted:
ObscureCoder
  • Resident Elite
Status: Offline
Joined: Jun 29, 201311Year Member
Posts: 211
Reputation Power: 13
Status: Offline
Joined: Jun 29, 201311Year Member
Posts: 211
Reputation Power: 13
Here's your program, fixed.. without the joke bit since you need to do system specific sleep calls. As for the enum, you can define HAPPY = 1 and leave the rest blank and the compiler will infer incrementation. I didn't 'use' namespace standard because that's what scrubs do. I added in the standard input algorithm expected from beginner CLI programmers (and basically anyone doing CLI - removes repetition).

#include <iostream>

enum Choices {
    HAPPY = 1, PUNISH = 2, TEXTBOOK = 3, MAX_CHOICES
};

int main()
{
    int choice;
    std::cout << "Enter a number..." << std::endl;
    std::cin >> choice;


    while(choice == 0 || choice >= (MAX_CHOICES))
    {
        std::cerr << "Error, invalid choice. Try again." << std::endl;
        std::cin >> choice;
    }

    switch (choice) {
    case HAPPY:
        /*
         * You're gonna need to do whatever joke you're doing here
         * Note: you'll have to use the WinAPI Sleep() function to delay your program
         * I would've added it in but I use Linux and don't wanna add in usleep() or #ifdefs to confuse you.
         */
        break;
    case PUNISH:

        break;
    case TEXTBOOK:

        break;
    default:
        std::cerr << "Unexpected error occured." << std::endl;
        break;
    }

    // Optional: waits for keypress to finish execution.
    std::cin.ignore().get();

    return 0;
}

#6. Posted:
Taylor
  • Christmas!
Status: Offline
Joined: Apr 30, 201410Year Member
Posts: 5,962
Reputation Power: 15122
Status: Offline
Joined: Apr 30, 201410Year Member
Posts: 5,962
Reputation Power: 15122
ObscureCoder wrote Here's your program, fixed.. without the joke bit since you need to do system specific sleep calls. As for the enum, you can define HAPPY = 1 and leave the rest blank and the compiler will infer incrementation. I didn't 'use' namespace standard because that's what scrubs do. I added in the standard input algorithm expected from beginner CLI programmers (and basically anyone doing CLI - removes repetition).

#include <iostream>

enum Choices {
    HAPPY = 1, PUNISH = 2, TEXTBOOK = 3, MAX_CHOICES
};

int main()
{
    int choice;
    std::cout << "Enter a number..." << std::endl;
    std::cin >> choice;


    while(choice == 0 || choice >= (MAX_CHOICES))
    {
        std::cerr << "Error, invalid choice. Try again." << std::endl;
        std::cin >> choice;
    }

    switch (choice) {
    case HAPPY:
        /*
         * You're gonna need to do whatever joke you're doing here
         * Note: you'll have to use the WinAPI Sleep() function to delay your program
         * I would've added it in but I use Linux and don't wanna add in usleep() or #ifdefs to confuse you.
         */
        break;
    case PUNISH:

        break;
    case TEXTBOOK:

        break;
    default:
        std::cerr << "Unexpected error occured." << std::endl;
        break;
    }

    // Optional: waits for keypress to finish execution.
    std::cin.ignore().get();

    return 0;
}



To be completely honest. The code you provided looks completely foreign to me, I've never done some of those things.

I will try to execute it tomorrow, at school.

If you'd like, you could PM me your email (Has to be Gmail, because I would share them via Google Docs) and I could share you some of my past projects, that worked. They're screen shots in the actual program, and not just typed code on a forum. It might give you an idea of what I'm used to doing.

I really do appreciate all the help though.
Jump to:
You are viewing our Forum Archives. To view or take place in current topics click here.