Still learning and I just want to be sure I'm clear on a few very basic syntax issues. Any help is much appreciated.

5.1 - Basics
int = a single number?
char = a single letter OR number?
string = a phrase of letters (up to?)

char array[]; // array delcared
array[4]; // fifth element in an array
*array[4]; // pointer to 5th content of array.

vector<string> guitars(); //created a vector container for guitars
vector<string> guitars(10, "cats");//created 10 vector container each container set to 'cats'.

5.2 - , vs ; and '' vs ""
*when do I decide to use commas or colons? I realize that's a pretty obtuse question but is there a general rule or way to think of it? Or should I just memorize every syntax to know when to use which?

*Same question for single or double quotes. Are double quotes ONLY ever for strings?

*what is the difference between these?

char num1 = 64;
char num1 = '64';

5.3 - string OBJECT vs string LITERAL
Can you explain the difference between these. I feel like I 'almost' get it.

5.2: Every c++ statement ends with a semicolon. Commas are only used to separate multiple statement on a line.

For example:

int a;
int b;
a = 0;
b = 1;

The above could also be written like below. Which one to use is up to you -- the compiler doesn't care.

int a, b;
a = 0, b = 1;

Double quotes are used to enclose strings, single quotes are used to enclose a single character

string World = "Hello World";
char c = 'H';

The compiler treats everything within double quotes as a null-terminated string, or character array. So that second statement char c = "H"; is wrong and will produce a compiler error message.

>>*what is the difference between these?
The first is a numeric value 64. The second is syntax error because you can not have two or more characters within single quotes.

A string literal is a set of characters within double quotes, such as "Hello World". A string object is just the name you give to the array string World;

@Parse

Sorry, I have the charts that list the characters, but it all seems so abstract. I'm just looking for some practical/typical uses of each to get me thinking of them in the right direction. I know it doesn't HAVE to be a letter, or number etc but I'm just trying to get the jist of 'typically' you will use this for that, etc.

char c; // get input from user like 'y' or 'n' single get character, great for asking questions.

char yn;
cout << "Would you like to exit?: " cin >> yn;
or
cout << "I will assume your age"; int age = 25;
cout << "Your age is: " << age;
int age;
cout << "How old are you?: "; cin >> age;
string myString = "I will now pull on your gString";
cout << myString;
char anArray[4] = {"a","b","c","d","e"};
int i;

for (i = 0; i < 5; i++)
cout << anArray[i];
char anArray[4] = {"a","b","c","d","e"};
int i;

for (i = 4; i < 5; i++)
cout << anArray[i]; // i could be mistaken

this is all i can help with im too drunk and my head hurts to even proceed =P

>>*array[4]; // pointer to 5th content of array.

The above is correct only if array is declared as DataType*** array ,
because you deference it twice. If you declare array as DataType array[] then *array[4] is invalid syntax, because you are using the dereferencing operator on a DataType, and from your post, DataType is something like int,char, or some POD.

>>what is the difference between these?
char num1 = 64;
char num1 = '64';

Try it, the second one is a compiler error.

p@arse
thank you drunken master...


firstPerson/Ancient Dragon
so the single quotes are only used for single characters (ONE of the 256)?

p@arse
thank you drunken master...


firstPerson/Ancient Dragon
so the single quotes are only used for single characters (ONE of the 256)?

yea grasshopper, although its not necessarily limited to 256.

So this has been helping a bunch. Hope you don't mind if I have a new round of questions:

5.4 - to Inline, or not to Inline.
If inlining a question is decided by compiler, why not default inline ALL functions?

5.5 - Answered

5.6 - Multiple Return statements in a function.
I heard that you can have multiple return statements within a single function. Could someone show me an example of this, and explain to me how the program knows not to exit the function after the first one?
...also why would you ever want to do this? It sounds like it's asking for trouble no?

5.7 - Function returning value or variable?
In this example, is the function returning the actual variable num, or is it returning the int value of num?
If yes or no, does return num as a var if num is a global var?

int askNumber(string prompt)
{
    int num;
    cout << prompt;
    cin >> num;
    return num;
}

5.4: inline makes no guarantee on what happens with the function. The compiler decides
what will happen to all function, whether to inline or not.

5.6: No its not possible, although you can always use a struct wrapper.

5.7: what happens there is, when you return num, the compiler makes a copy of num
and stores it in some temporary address, then retrieves it if needed.

5.6 - Multiple Return statements in a function.
I heard that you can have multiple return statements within a single function. Could someone show me an example of this, and explain to me how the program knows not to exit the function after the first one?
...also why would you ever want to do this? It sounds like it's asking for trouble no?

You can return multiple values but you dont actually return them, what i think they might be geting at there is using multiple inputs as returns such as,

/* the following function expects that you will pass an array of ints and a count of the number of ints*/
void squareSomeNumbers(int numberOfNumbers, int *numbers)
{
  for(int x = 0; x< numberOfNumbers; x++)
  {
    *(numbers+x)= (*(numbers+x)) * (*(numbers+x)); 
   /* possibly not the best or clearest example here but whats happening is
      that *(numbers+x) means the data (value) in the array position x 
      just like numbers[x] = numbers[x] * numbers[x]
   */
  }

}

The above function as it uses pointers will edit the original variables passed to it and afterwards these are changed so you can maybe consider that returning multiple variables from one function but maybe not depends how you want to look at it, thats all i can think of for how someone could say you can return multiple values from a function without returning a structure or class.

5.6 is not talking about returning multiple values but coding multiple return statements

if( condition1 )
   return 1;
if( condition2 )
   return 2;
// etc
commented: great answers! +1

5.4: Is the compiler just kind of unreliable, or is this similar to watching out for logical errors?

5.7: but doesn't num not exist outside the function scope? also, is it storing num, or the value of num?

Thanks for this! Huge help!

Also,

I pasted this block of code in above my main() function to make these all global variables, but I keep getting the error:

expected constructor, destructor, or type conversion before'.' token.
expected ',' or ';' before '.' token.

This is the block I'm trying to paste in as global variables. They work inside my main() function, but not outside it.

//setup
    const int MAX_WRONG=8;   //maximum number of incorrect guesses allowed
    vector<string> words;    //collection of possible words to guess
    words.push_back("GUESS");
    words.push_back("HANGMAN");
    words.push_back("DIFFICULT");
    
    srand(time(0));
    random_shuffle(words.begin(), words.end());
    const string THE_WORD = words[0];          //word to guess
    int wrong = 0;                             //number of incorrect guesses
    string soFar(THE_WORD.size(), '-');        //word guessed so far        //???How does this soFar declaration, know what to do with the arguments passed? I'm not even making a function here.
    string used = "";                          //letters already guessed

RE: ABOVE
the 'words.push_back' lines and random shuffle seemed to be the parts that didn't like going global, so I put all but them into global, and worked my code as follows, but the program seems to have a logic error (runs infinite, turns gray, crashes). Any ideas what I'm doing wrong here? This goes hand in hand with my problem above.

//HANGMAN_5editionv2
//Re-making the hangman game from chapter 5 with functions.
//Include a function to take players guess, and another to check and see if their guess is the secret word.

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <ctime>
#include <cctype>

using namespace std;

char guess;
vector<string> words;    //collection of possible words to guess
const string THE_WORD = words[0];          //word to guess
string soFar(THE_WORD.size(), '-');        //word guessed so far        //???How does this soFar declaration, know what to do with the arguments passed? I'm not even making a function here.
int wrong = 0;                             //number of incorrect guesses

char getGuess();
char checkWord();

int main()
{
    //setup
    const int MAX_WRONG=8;   //maximum number of incorrect guesses allowed

    words.push_back("GUESS");
    words.push_back("HANGMAN");
    words.push_back("DIFFICULT");
    
    srand(time(0));
    random_shuffle(words.begin(), words.end());



    string used = "";                          //letters already guessed
    
    cout << "Welcome to Hangman. Good Luck!\n";
    
    
    //main game loop
    while ((wrong < MAX_WRONG) && (soFar != THE_WORD))
    {
          cout << "\n\nYou have " << (MAX_WRONG - wrong) << " incorrect guesses left.\n";
          cout << "\nYou've used the following letters:\n" << used << endl;     //???How is used finding which letters are guessed.
          cout << "\nSo far, the word is: \n" << soFar << endl;
          
          
          //FUNCTION to get users guess
          getGuess();
          
          while(used.find(guess) != string:: npos)
          {
                                 cout << "\nYou've already guessed " << guess << endl;
                                 cout << "Enter your guess: ";
                                 cin >> guess;
                                 guess = toupper(guess);
          }
          
          used += guess;
          
          //FUNCTION to check to see if users word is THE_WORD
          checkWord();
 }
    
    //shut down
    if (wrong == MAX_WRONG)
       cout << "\nYou've been hanged!";
    else
        cout << "\nYou guessed it!";
        
    cout << "\nThe word was " << THE_WORD << endl;
    
    system("PAUSE");
    return 0;
    
}

char getGuess()
{
          cout << "\n\nEnter your guess: ";
          cin >> guess;
          guess = toupper(guess); //make uppercase since secret word in uppercase

 }


char checkWord()
{
       if(THE_WORD.find(guess) != string::npos)
          {
                                  cout << "That's right! " << guess << " is in the word.\n";
                                  
                                  //update soFar to include newly guessed letter
                                  for(int i=0; i < THE_WORD.length(); ++i)
                                          if(THE_WORD[i] == guess)
                                                         soFar[i] = guess;
          }
          else
          {
              cout << "Sorry, " << guess << " isn't in the word.\n";
              ++wrong;
          }

}

You should be aware by now that you can only put executable code inside a function. push_back() is executable code. vector<string> words is just an object declaration and can go either inside a function or globally outside a function.

Think about what you are doing, instead of just randomly tossing ink at a piece of paper and hoping something useful will result. Much like the infinite monkey theorem

Nope, didn't cover that yet. Thanks for letting me know. I'll try that out after I finish my banana. Want one?

Reason I tried it is that I assumed push_back() was a function that was globally declared because of #include <vector>, so it would be accessible. I didn't KNOW for sure though - that's why I tried it. When that didn't work i pasted the entire block of variables hoping to weed out the culprit by removing them one by one as I received errors. Is there a better way to troubleshoot than that? If I was already aware of these things I wouldn't ask for help. I thought that's what the forum was for.

Im probibally not the most expeirenced person or best to say this but i think what ancient dragon is geting at there is, that you should use a more analyitcal method about fixing issues, look at the code and try to think why does it not work and solve the problem that way instead of "hacking code" as my lecturer would have put it by just changing bits untill it works.

Like I said before, I DID try to figure it out but couldn't, then pasted the block and returned things to their position to try and weed out the problem. I don't know a lot about code, so my scope of analysis is pretty limited still. It's kind of a bummer when people assume because you're not as smart as them, you must not be trying... It's pretty frustrating when you're trying to learn, hit a wall, ask for help, then instead of spending the time helping, they put their energy into calling you a dumb monkey(tear..). It's not my fault that I love banana's. They're delicious okay!

Anyway, if anyone has a clue what's wrong with the code above I'm still all ears. I'd like to keep this about programming, specifically the questions above.

Well as you took that code from beginning c++ game programming i suggest that you revert to the original solution and try making changes one by one, if you have the book read that section again for a better understanding of what is happening.

For example you are making functions getGuess and checkWord make sure they work.

When i ran your code its bugs galore, it does not store incorrect guesses right anymore, it says you have already guessed when you have not etc etc.

Go back to the original code and make small changes one by one untill you get the changes you want :)

I'm on pass #3 of the entire chapter. I've been SCRUBBING it but a lot of the things I'm hearing brought up here we haven't covered yet. I wonder if they assume we shouldn't be asking that many questions yet.

5.6 Multiple return statements can happen in a function if used with a conditional.
Take for example:

#include <iostream>
using namespace std;

//Allows or disallows a person into a bar, dependant on their age
char* enterBar(int age)
{
    if (age >18)
    {
        return "You may enter the bar.";
    }
    else
    {
        return "You cannot enter the bar. You are too young.";
    }
}

int main()
{
    int age;
    cout << "How old are you? "; //Ask user for age
    cin >> age; //Receive user's age
    cin.ignore(); //Prevent window from closing
    cout << enterBar(age); //Is the user old enough to drink?
    cin.get(); //Allow programmer to view output until pressing enter
}

is a fine example, you could also do it with integers, I just used a character pointer(string), because it's all I had in mind at the time. If I misunderstood you and you meant returning multiple values all at once, you would have to pass arguments by reference or by pointer instead of by value, you can not use return more than once unless used with conditionals, to my knowledge. :)
I hope this helps..

Can you explain/verify the difference between a reference and a pointer. Aren't they the same thing - just one is for array and the other is for vector?

The do exactly the same thing. The only difference is the way they are dereferenced, such as -> for pointers and . for references. Another difference is the way the sending function uses them, whether it uses & to create a pointer.

A vector can be passed by either pointer or reference.

A reference is a variable memory address (where the variable is stored in memory). It'd be a hexidecimal number, so if you tried to cout a reference variable you'd get something incomprehensible on the output.

A pointer is a variable that holds a reference. Whatever you do to the pointer will also happen to the object it's being referenced to.

Pointers are also important for dynamic memory, which is a very powerful concept of C++ that allows memory to be allocated during run-time instead of before.

That is probably just as confusing as what I posted. Here is a link that better explains references and pointers.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.