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

  1. #16
    Registered User
    Join Date
    Jan 2012
    Posts
    11
    What I think I'm trying to do is; Dynamically determine the 'cout' stuff based on user selection/input rather than writing out every line of cout. I'm thinking like that, because I thought that I need this approach when I later create the actual questions and answers portion, hopefully with possible answers (multiple-choice) being in random order for each question, as well as questions being displayed randomly. Getting the correct series of questions from the input/selections made.

    Anyways, so basically if I understand correctly, I should scale-back more straightforward cout what's needed approach? Or am I just doing something fundamentally wrong?

    PS: yeah... the error stuff I still need to clean up. It's difficult for me to stray from my minimal html coding approaches (i guess that's how i'd have to call it).

  2. #17
    Registered User
    Join Date
    Jan 2012
    Posts
    11
    Ok, so I'm still trying to get a hang of all these different things I'm learning and obviously not likely making good programing sense yet, but I was wondering if anybody got some good pointers on what I've done so far.

    Code:
    #include <iostream>
    
    
    using namespace std;
    
    
    struct error {
    	string invalid;
    };
    
    
    int main()
    {
    	// Initializing struct error for variable selection
    	error selection;
    		// Text for selection in invalid
    		selection.invalid = "--- Invalid selection! ---";
    
    
    	// Adding Section & Chapter text to array 'lable'
    	string lable[4][6];
    		// Overall header/title
    		lable[0][0] = "Quiz: The basics of C++";
    
    
    		// Section titles
    		lable[1][0] = "Introduction and Basic C++ Features";
    		lable[2][0] = "Pointers, Arrays and Strings";
    		lable[3][0] = "File IO, command line arguments and intro to classes";
    
    
    		// Chapter titles - Section 1
    		lable[1][1] = "Intro";
    		lable[1][2] = "If Statements";
    		lable[1][3] = "Loops";
    		lable[1][4] = "Functions";
    		lable[1][5] = "Switch case";
    
    
    		// Chapter titles - Section 2
    		lable[2][1] = "Accessing Memory with Pointers";
    		lable[2][2] = "Structures in C++";
    		lable[2][3] = "Storing data with Arrays";
    		lable[2][4] = "Character Strings in C++";
    		lable[2][5] = " ";
    
    
    		// Chapter titles - Section 3
    		lable[3][1] = "File I/O";
    		lable[3][2] = "Typecasting";
    		lable[3][3] = "Classes and introduction to object-oriented programming";
    		lable[3][4] = "Inline functions";
    		lable[3][5] = "Command line arguments";
    
    
    	// Inistializing string(s)
    	string title;
    		title = lable[0][0];
    
    
    	// Initializing integers and setting default values
    	int x;
    	int y;
    	int z = 5;
    	int section_id = 0;
    	int chapter_id = 0;
    	int sctn = 0;
    	int chptr = 0;
    
    
    	cout << title << endl << endl;
    
    
    	while (sctn == 0 || sctn > 3)
    	{
    		// Get Section text for selection and asign selection
    		cout << "Please select a section (1-3):" << endl;
    		for (x=1; x<4; x++)
    		{
    			y=chptr;
    			section_id = x;
    			cout << section_id << ". " << lable[x][y] << endl;
    		}
    		cin >> sctn;
    		cin.ignore();
    		// change z depending on nr of chapter in section
    		if (sctn==2) {z=4;}
    
    
    		// Error check
    		if (sctn == 0 || sctn > 3)
    		{
    			cout << selection.invalid << endl << endl;
    		}
    	}
    
    
    	while (chptr == 0 || chptr >= z)
    	{
    		// Get Section text for selection
    		cout << endl << endl;
    		cout << lable[sctn][y] << endl;
    
    
    		// Get Chapter text for selection and asign selection
    		cout << endl << endl;
    		cout << "Please select a chapter (1-" << z << "):" << endl;
    
    
    		for (y=1; y<=z; y++)
    		{
    			x=sctn;
    			chapter_id = y;
    			cout << chapter_id << ". " << lable[x][y] << endl;
    		}
    		cin >> chptr;
    		cin.ignore();
    
    
    		// Error check
    		if (chptr == 0 || chptr >= z)
    		{
    			y = 0;
    			cout << selection.invalid << endl << endl;
    		}
    	}
    
    
    	if (sctn!=0 && chptr!=0)
    	{
    		// Temporary selection output
    		cout << "----------------------------------" << endl << endl;
    		cout << "You have selected section-number: " << sctn << endl;
    		cout << "The title of the section is: " << lable[sctn][0] << endl;
    		cout << endl << endl;
    		cout << "You have selected chapter-number: " << chptr << endl;
    		cout << "The title of the chapter is: " << lable[sctn][chptr] << endl;
    		cout << "----------------------------------" << endl << endl;
    		cout << "The array-code is: lable[" << sctn << "][" << chptr << "]" << endl;
    	}
    }
    Please keep in mind, that I'm practicing all the different things in the tutorials and try to find ways to use them, even when it's not necessarily needed. I also think that, if I understand it correctly, my use of the array is probably something a coder shouldn't do, do to memory usage?

    Well, at least I'm happy to be starting to write code without having to look at the examples so much anymore :P

    edit: oh, i suppose i don't need the last If statement, do I?
    Last edited by nGAGE; 01-19-2012 at 09:38 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