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

Threaded View

Previous Post Previous Post   Next Post Next Post
  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.

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