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?