Thread: New to c++, could use some guidance

  1. #1
    Registered User
    Join Date
    Jan 2012
    Posts
    11

    New to c++, could use some guidance

    Hi all,

    a couple of days ago I started going through the C++ tutorials and while trying some code of my own I've run into some oversight (I suppose) that I just don't spot yet.

    As I was going through the various topics in the tutorials I figured that it might be good to not just copy code but do something with the stuff I learn (hopefully).
    So I thought, why not make the 'quiz' at the end of each chapter in c++.

    Now, I realize that I will still need to learn and build much more complex stuff, put them in functions etc to make it all work, but the basic idea is; for it all eventually to become rather dynamic so I can add chapters with new questions and answers later on etc.

    Anyways,... I'm still very early in the learning process and I'm already running into something I'm not quite figuring out. I could use a few pointers. And no, not the ones that refer to memory addresses.

    Here's the code I got:
    Code:
    #include <iostream>
    
    
    using namespace std;
    
    
    int main()
    {
        string section("Introduction");
        string title("Quiz: The basics of C++");
        string chapter;
        string chapter1("Intro");
        string chapter2("If Statements");
        string chapter3("Loops");
        string chapter4("Functions");
        string chapter5("Switch case");
        string ERR("--- Sorry! Something went wrong. ---");
        string ERR2("--- Sorry! Please select a number from 1-5. ---");
    
    
    
        int chptr;
    
    
        if (chptr == '0' || chptr >= '6')
        {
            chapter = ERR;
        }
        else if (chptr == '1')
        {
            chapter = chapter1;
        }
        else if (chptr == '2')
        {
            chapter = chapter2;
        }
        else if (chptr == '3')
        {
            chapter = chapter3;
        }
        else if (chptr == '4')
        {
            chapter = chapter4;
        }
        else if (chptr == '5')
        {
            chapter = chapter5;
        }
        else
        {
            chapter = ERR2;
        }
    
    
        cout << title << endl << endl;
        cout << "Please select a chapter (1-5)" << endl;
        cin >> chptr;
        cin.ignore();
    
    
        cout << "You have selected chapter-number: " << chptr << endl;
        cout << endl << "The title of the chapter is: " << chapter << endl;
    
    
    }
    For some reason, variable chptr is returned correctly in the second to last cout, but it doesn't display the string of text I want 'chapter' to become.

    I've already tried moving things around a bit, putting variables outside of main or trying to have 'chapter' be a simple function, but when I do that I actually run into errors. I guess I need to re-read the functions chapter. The above code doesn't give me any errors, but it just doesn't do want I want it to do.

    I'd very much appreciate some useful pointers to help me understand where I'm going wrong and why.

    I hope I'm explaining correctly what I can't figure out right here. Everything else I haven't figured out, such as how to loop this whole thing upon incorrect input I'll do later, hopefully.


    Thanks in advance.

    EDIT: Oh yeah... and I have tried moving the If statement after the cin, but that just gives me the second error strong ERR2 returned instead of the first, which I find even more odd, considering that chptr still outputs as a int value.
    Last edited by nGAGE; 01-16-2012 at 06:57 AM.

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Where have you initialized your variable chptr? You must always assign variables values before you can use them. Also chptr is defined as being an int, yet you try to compare this int to a constant character ('1', '2', etc.). So when you compare your int to this character the number 5 is not equal to '5' . '5' is equal to 53, which is the ASCII value of the character '5'.

    Jim

  3. #3
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    I can see your problem immediately, but I'm going to help you find it yourself. what operating system and compiler are you using?

    if you are using GCC as your compiler (linux, code::blocks on windows), you can add -Wall and -Wextra to your command line, and it will tell you exactly what the problem is in the code you posted.

    the reason you're getting ERR2 is because when you declare chptr as an int, and then compare it to a char constant, it will happily do it, because C++ does implicit conversions between all integral types. the problem is that entering '1' for chptr puts the integer value 1 in chptr, but you're checking for the character value '1'.

  4. #4
    Registered User
    Join Date
    Jan 2012
    Posts
    11
    Gotcha. I wasn't quite sure about the meaning/difference of constant and int when the value is visually as written the same, but figured out so far that a char would be a constant needing the single ' qotes when comparing whereas numbers don't. Correct?

    Anyways, it seems to work now (i think). Here's what I made of it:
    Code:
    #include <iostream>
    
    
    using namespace std;
    
    
    int main()
    {
        string section("Introduction");
        string title("Quiz: The basics of C++");
        string chapter;
        string chapter1("Intro");
        string chapter2("If Statements");
        string chapter3("Loops");
        string chapter4("Functions");
        string chapter5("Switch case");
        string ERR("--- Sorry! Please select a number from 1-5. ---");
    
    
        int chptr = 0;
    
    
        cout << title << endl << endl;
        cout << section << "..." << endl;
    
    
        cout << "Please select a chapter (1-5)" << endl;
        cout << "1 - " << chapter1 << endl;
        cout << "2 - " << chapter2 << endl;
        cout << "3 - " << chapter3 << endl;
        cout << "4 - " << chapter4 << endl;
        cout << "5 - " << chapter5 << endl;
    
    
        cin >> chptr;
    	cin.ignore();
    
    
        if (chptr == '0' || chptr >= '6')
        {
            chapter = ERR;
        }
        else if (chptr == 1)
        {
            chapter = chapter1;
        }
        else if (chptr == 2)
        {
            chapter = chapter2;
        }
        else if (chptr == 3)
        {
            chapter = chapter3;
        }
        else if (chptr == 4)
        {
            chapter = chapter4;
        }
        else if (chptr == 5)
        {
            chapter = chapter5;
        }
        else
        {
            chapter = ERR;
        }
    
    
        cout << "You have selected chapter-number: " << chptr << endl;
        cout << endl << "The title of the chapter is: " << chapter << endl;
    
    
    
    
    }
    Thanks for guiding me in the right direction and not just putting up the code. I wanna learn and understand the 'why'! Awesome help! Much appreciated.

  5. #5
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Quote Originally Posted by nGAGE View Post
    a char would be a constant needing the single ' qotes when comparing whereas numbers don't. Correct?
    yes, and you still have single quotes on this line:

    Code:
    if (chptr == '0' || chptr >= '6')
    they should not be there.

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    You do not actually need the first if/else block, you check all the relevant numbers below that and you end with an else statement which will accomplish this check.
    Code:
         if (chptr == '0' || chptr >= '6') // Should be integers but this if is not needed.
        {
            chapter = ERR;
        }
        else

    Jim

  7. #7
    Registered User
    Join Date
    Jan 2012
    Posts
    11
    Woops.... oversight, again. Fixed. Thnx

    Now is my assumption correct that I can now use the If statement blocks to add calls to functions that will actually do the displaying of questions, multiple-choice answers and determine if input was correct for selected chapter? At least, so far that's what my initial thought was on how I should do it.

  8. #8
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    I don't know if you've gotten to the lesson on switch statements yet, but this would be a good opportunity to use a switch.

  9. #9
    Registered User
    Join Date
    Jan 2012
    Posts
    11
    Yes I have. Thnx, I'll have a look at that. It's still very hard for me to know, when and which approach is most efficient. These tips are very helpful, thnx

  10. #10
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    a switch is useful any time you have more than about 3 integer values to select from. it eliminates all the else if... blocks and makes the code a bit easier to read. it's especially helpful if you have an enumeration that you're testing against.

  11. #11
    Registered User
    Join Date
    Jan 2012
    Posts
    11
    Alright, so here's where I am now. I know I still gotta find better ways for things, but is this roughly what you meant with using the 'switch' stuff?

    Code:
    #include <iostream>
    
    
    using namespace std;
    
    
    int main()
    {
        // Initializing strings: Menu-selection
        string title("Quiz: The basics of C++");
    
    
        string section;
        string section1;
        string section2;
        string section3;
    
    
        string chapter;
        string chapter1;
        string chapter2;
        string chapter3;
        string chapter4;
        string chapter5;
    
    
        // Initializing strings: Error returns
        string ERR("--- Sorry! Please select a number from 1-5. ---");
    
    
        // Initializing Integers: Section/chapter selection
        int sctn = 0;
        int chptr = 0;
        int x = 4;
    
    
        // Defining section strings
        section1 = "Introduction and Basic C++ Features";
        section2 = "Pointers, Arrays and Strings";
        section3 = "File IO, command line arguments and intro to classes";
    
    
        // Section-selection menu
        cout << title << endl;
        cout << endl;
        cout << "Please select a section (1-3)" << endl;
        cout << "1 - " << section1 << endl;
        cout << "2 - " << section2 << endl;
        cout << "3 - " << section3 << endl;
        cin >> sctn;
        cin.ignore();
    
    
        // Selecting Section
        switch (sctn)
        {
        case 1:
            section = section1;
            // Defining chapters Section1 - Introduction and Basic C++ Features
            chapter1 = "Intro";
            chapter2 = "If Statements";
            chapter3 = "Loops";
            chapter4 = "Functions";
            chapter5 = "Switch case";
    
    
            break;
        case 2:
            section = section2;
            // Defining chapters Section2 - Pointers, Arrays and Strings
            chapter1 = "Accessing Memory with Pointers";
            chapter2 = "Structures in C++";
            chapter3 = "Storing data with Arrays";
            chapter4 = "Character Strings in C++";
            chapter5 = " ";
            break;
        case 3:
            section = section3;
            // Defining chapters Section3 - File IO, command line arguments and intro to classes
            chapter1 = "File I/O";
            chapter2 = "Typecasting";
            chapter3 = "Classes and introduction to object-oriented programming";
            chapter4 = "Inline functions";
            chapter5 = "Command line arguments";
            break;
        default:
            cout << "--- Something went wrong ---";
            break;
       }
    
    
        cout << endl;
        if ( chapter5 != " " )
        {
            x = 5;
        }
        cout << "Please select a chapter (1-" << x << ")" << endl;
        cout << "1. - " << chapter1 << endl;
        cout << "2. - " << chapter2 << endl;
        cout << "3. - " << chapter3 << endl;
        cout << "4. - " << chapter4 << endl;
        if ( chapter5 != " " )
        {
            x = 5;
            cout << "5. - " << chapter5 << endl;
        }
        cin >> chptr;
        cin.ignore();
    
    
        switch (chptr)
        {
        case 1:
            chapter = chapter1;
            break;
        case 2:
            chapter = chapter2;
            break;
        case 3:
            chapter = chapter3;
            break;
        case 4:
            chapter = chapter4;
            break;
        case 5:
            chapter = chapter5;
            break;
        default:
            chapter = ERR;
            break;
        }
    
    
        cout << endl << endl;
        cout << "You have selected section-number: " << sctn << endl;
        cout << "The title of the section is: " << section << endl;
    
    
        cout << endl << endl;
        cout << "You have selected chapter-number: " << chptr << endl;
        cout << "The title of the chapter is: " << chapter << endl;
    
    
    }
    Honestly, I gotta figure out how to do a proper dynamic numbering thingy for the selections, but I guess it does display what I want it to display at the moment.
    Last edited by nGAGE; 01-17-2012 at 06:29 AM.

  12. #12
    Registered User
    Join Date
    Jan 2012
    Posts
    11
    Hmm... I just started reading through the structures chapter and now I'm starting to think that maybe that's the way to do my section/chapter selections? Also, I'm not quite sure if I should worry about 'pointers' yet or not. How important is it for me to start using them right from the beginning when memory isn't an issue yet? I find it very confusing because of all the back and forth thinking I'd need to be doing to keep track of what is what. Or do you have some tips on how to understand making the process easy to follow, and of course, clear to know when I should use it and when it's overkill?

  13. #13
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Do you need to have some variable outlive the scope of a function?
    Do you need to dynamically initialize some reference (that is, assign some value to it other than at declaration time)?

    If you answer yes to any of those, pointers are good candidates. Especially the first.

    The basic rule of thumb is: if you don't need to use pointers, then don't. Do it without pointers first, and if it appears impossible, only then switch to pointers.
    Usually pointers are not so much needed in C++. But you will learn when you have to use them with time.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  14. #14
    Registered User
    Join Date
    Jan 2012
    Posts
    11
    I guess I'm not quite following the thought-process yet. I'm already getting confused trying to change the code with new stuff I'm going through in the tutorials. It's all starting to look like a jumbled mess, though it does seem to work (for the most part).

    Current code:
    Code:
    #include <iostream>
    
    
    using namespace std;
    
    
    struct select {
        int id;
        string lable;
    };
    
    
    int main()
    {
        // Initializing strings: Menu-selection
        string title("Quiz: The basics of C++");
    
    
        // Initializing strings: chapter selection
        string chapter1;
        string chapter2;
        string chapter3;
        string chapter4;
        string chapter5;
    
    
        // Initializing Integers: Section/chapter selection
        int sctn = 0;
        int chptr = 0;
        // Initializing Error codes (0 = no errors)
        int ERR = 0;
        // Initializing Integers: Section/chapter count
        int x = 5;
        int s = 1;
        int c = 1;
    
    
        // Defining section strings
        string section1 = "Introduction and Basic C++ Features";
        string section2 = "Pointers, Arrays and Strings";
        string section3 = "File IO, command line arguments and intro to classes";
    
    
        // Setting up STRUCT variables
        select section;
        select chapter;
    
    
        // Section-selection menu
        cout << title << endl;
        cout << endl;
        cout << "Please select a section (1-3)" << endl;
    
    
        while ( s < 4 )
        {
            section.id = s;
                switch (section.id)
                {
                    case 1:
                        section.lable = section1;
                        break;
                    case 2:
                        x = 4;
                        section.lable = section2;
                        break;
                    case 3:
                        section.lable = section3;
                        break;
                    default:
                        cout << "--- Something went wrong ---" << endl;
                }
            cout << section.id << ". " << section.lable << endl;
            s++;
        }
        cin >> sctn;
        cin.ignore();
    
    
        if (sctn == 0 || sctn > 3)
        {
            ERR = 1;
            cout << "--- Something went wrong ---";
        }
        else
        {
            // Selecting Section & setting chapter strings
            switch (sctn)
            {
            case 1:
                section.lable = section1;
                // Defining chapters Section1 - Introduction and Basic C++ Features
                chapter1 = "Intro";
                chapter2 = "If Statements";
                chapter3 = "Loops";
                chapter4 = "Functions";
                chapter5 = "Switch case";
                break;
            case 2:
                section.lable = section2;
                // Defining chapters Section2 - Pointers, Arrays and Strings
                chapter1 = "Accessing Memory with Pointers";
                chapter2 = "Structures in C++";
                chapter3 = "Storing data with Arrays";
                chapter4 = "Character Strings in C++";
                break;
            case 3:
                section.lable = section3;
                // Defining chapters Section3 - File IO, command line arguments and intro to classes
                chapter1 = "File I/O";
                chapter2 = "Typecasting";
                chapter3 = "Classes and introduction to object-oriented programming";
                chapter4 = "Inline functions";
                chapter5 = "Command line arguments";
                break;
            default:
                ERR = 1;
                cout << "--- Something went wrong ---";
                break;
            }
        }
    
    
        if ( ERR == 0 )
        {
            cout << endl;
            cout << "Please select a chapter (1-" << x << ")" << endl;
            while ( c <= x )
            {
                chapter.id = c;
                switch (chapter.id)
                {
                    case 1:
                        chapter.lable = chapter1;
                        break;
                    case 2:
                        chapter.lable = chapter2;
                        break;
                    case 3:
                        chapter.lable = chapter3;
                        break;
                    case 4:
                        chapter.lable = chapter4;
                        break;
                    case 5:
                        chapter.lable = chapter5;
                        break;
                    default:
                        ERR = 1;
                        cout << "--- Something went wrong ---" << endl;
                }
                cout << chapter.id << ". " << chapter.lable << endl;
                c++;
            }
            cin >> chptr;
            cin.ignore();
        }
        if (chptr == 0 || chptr > x)
        {
            ERR = 1;
            cout << "--- Something went wrong ---";
        }
        else
        {
            switch (chptr)
            {
            case 1:
                chapter.lable = chapter1;
                break;
            case 2:
                chapter.lable = chapter2;
                break;
            case 3:
                chapter.lable = chapter3;
                break;
            case 4:
                chapter.lable = chapter4;
                break;
            case 5:
                chapter.lable = chapter5;
                break;
            default:
                chapter.lable = ERR;
                break;
            }
    
    
            cout << endl << endl;
            cout << "You have selected section-number: " << sctn << endl;
            cout << "The title of the section is: " << section.lable << endl;
    
    
            cout << endl << endl;
            cout << "You have selected chapter-number: " << chptr << endl;
            cout << "The title of the chapter is: " << chapter.lable << endl;
        }
        /*
        cin.get();
        */
    }
    So Now, I'm trying to figure out how to use 'arrays' to store all the different strings of text. But I'm starting to get confused as to how I'm supposed to do it.

    The thing that I keep scratching my head at is the varying chapter# variables that contain different strings depending on the section# choice. I do hope that I've setup the structs correctly, so that on the acutal quiz portion I'll be able to call the correct questions/answers for selected section and chapter with the section.id & chapter.id.
    I don't see an easier/cleaner way then what I'm doing (only alternative, but equally messy ways).

    Maybe I should continue with the tutorials and see if anything that follows will make me see/understand cleaner methods?
    Last edited by nGAGE; 01-17-2012 at 01:28 PM.

  15. #15
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    It looks like you are trying to do everything in parallel. I suggest you do it serially.
    That is to say, if the player chooses X, then do all the stuff down that path. What you are doing is basically store player did X, fill structs of info, store player did Y, etc.

    Also, if some error occurs, you can just return. That will save you form having to check if err==0. Or you can throw an exception if you want to look into that.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need some guidance.
    By Kinto in forum C Programming
    Replies: 10
    Last Post: 05-31-2009, 12:02 AM
  2. Guidance, please.
    By mattagrimonti in forum C Programming
    Replies: 2
    Last Post: 11-26-2008, 08:50 AM
  3. In need of guidance
    By Xk4r in forum C Programming
    Replies: 8
    Last Post: 11-24-2008, 07:10 PM
  4. I am in need of help, need C++ guidance
    By Chessman.exe in forum C++ Programming
    Replies: 51
    Last Post: 08-24-2007, 03:23 PM
  5. Little guidance please...
    By Jayhawk_26 in forum C Programming
    Replies: 5
    Last Post: 10-09-2006, 01:27 AM