Like Tree3Likes
  • 1 Post By bernt
  • 2 Post By kmdv

N00b with n00b question (probably a quick one)

This is a discussion on N00b with n00b question (probably a quick one) within the C++ Programming forums, part of the General Programming Boards category; Hi all, I had a multi-paragraph introduction typed out, but I decided that it was a bit much for what ...

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    7

    N00b with n00b question (probably a quick one)

    Hi all,

    I had a multi-paragraph introduction typed out, but I decided that it was a bit much for what is probably a really answer to a non-problem.

    The question: Why is it that if I call an array simply, "array", the text turns blue? This normally happens with key words, but with this one, it doesn't make sense to me.

    Thanks,

    -Rob

  2. #2
    Just a pushpin. bernt's Avatar
    Join Date
    May 2009
    Posts
    426
    My best guess is that it's because array is a data type in the C++11 standard library (in which case your text editor is more up-to-date than mine ). Do words like "set", "map", and "vector" also turn blue?

    EDIT:
    Ah! Just found something interesting here. Visual Studio does syntax highlighting for Microsoft's C++/CLI, which includes an 'array' keyword.
    In the case of Visual Studio this is as good an explanation as any. This would explain iMalc's 'event' keyword (below) being highlighted as well.
    Last edited by bernt; 11-04-2011 at 07:12 PM.
    hauzer likes this.
    Consider this post signed

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    Some syntax hilighters are a bit of a catch-all. Rather than making a syntax hilighter for C, C++ and the various versions thereof, some IDEs just have one syntax hlighter and use it for everything. I often find that the word 'event' is also hilighted like a keyword in MSVC.
    This forum is no different. If you post code in the C forum with a variable called 'new' in it, then it is also hilighted like a keyword.
    My advice is to simply not use those variable names if you want your code to look right in the IDE.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  4. #4
    Registered User
    Join Date
    Nov 2011
    Posts
    7
    Thanks for you answers. It doesn't seem to cause any problems if I call an array "array" even though the word turns blue, but it is confusing to look at. I'll take your advice and call them anything but that.

    Now I have for you another question. It too probably has a very simple solution, but I can't figure out what's happening with this code.

    The program I'm writing (for a school assignment) should do the following things:
    1) Ask the user how many scores they would like to calculate (grades, average, smallest, largest)*
    2) Get a number of scores from the user
    3) Display the results

    It's a pretty basic program and I'm sure that most (if not all) of you have done it before. So here's the problem code:

    Code:
    for (int i=0; i<num_scores; i++)
        {
                cout << "Enter a score (or -1 if no more result): \n";
                cin >> input;
    
                if (input > 100)
                {
                    cout << "Please enter a score between 0 and 100: ";
                    cin >> input;
                    i--;
                }
    
                else if (input = -1)
                    break;
    
                else (input != -1 && input <= 100);
                {
                    sum++;
                    scores[i] = input;    //put scores into array
                }
        }
    The running program allows me to enter a single score before I get the "Press any key..." message. However, when I commented-out the else if statement, the program worked perfectly. I'm guessing that the problem is with the break statement. My understanding is that you can use the break statement to exit a loop when a particular condition is met (in this case, when input = -1). But, it seemed to be breaking from the loop even when -1 was not entered.

    *Note: The assignment sheet actually does not say that we have to do this. It is kind of vague: "You assume that there will be no more than 10 results (i.e. the user can enter 3 results, 6 results or a maximum of 10 results)". I realized just before writing the above paragraph that the -1 to exit (or sentinel, right?) is not necessary.

    Once again, thank you for baring with me.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,852
    > else if (input = -1)
    There is a difference between = and ==

    > else (input != -1 && input <= 100);
    The final else has no condition - this line shouldn't even compile.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    682
    Quote Originally Posted by Salem View Post
    >
    > else (input != -1 && input <= 100);
    The final else has no condition - this line shouldn't even compile.
    It can compile:
    Code:
    else
        (input != -1 && input <= 100);
    { // block, which always executes
        sum++;
        scores[i] = input;
    }
    Salem and Elysia like this.
    I never put signature, but I decided to make an exception.

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,025
    The semicolon might throw the compiler off... interesting.
    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.

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,852
    Bizarre.
    Not only does it compile, I can't get g++ to complain about an unused expression.

    I have to write the even more unpromising
    else input != -1 && input <= 100;
    to get a
    foo.cpp:27: warning: statement has no effect

    The only thing that matters is the ;, making it an empty statement. The rest is just vapour.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  9. #9
    Registered User
    Join Date
    Nov 2011
    Posts
    7
    I actually figured out the problem before returning to check for replies. Or perhaps more accurately, I figured out a working solution. That section of code now looks like this:

    Code:
    for (int i=0; i<num_scores; i++)         //for loop gets scores and puts them into the array
           {
                         cout << "Enter score (or -1 to end program): ";
                         cin >> input;
     
                         if (input > 100 || input < -1)
                         {
                               cout << "Please enter a score between 0 and 100: ";
                               i--;
                         }
                         else if (input != -1 && input <= 100)
                         {
                                sum += input;
                               scores[i] = input;                //put scores into array
                         }
                         else
                               exit(0);                                 //input of -1 ends the program
           }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Very sorry, but this must be the most n00b question =]
    By Teh_n00b in forum C++ Programming
    Replies: 24
    Last Post: 01-23-2007, 07:46 PM
  2. Visual C++ & DirectX, quick n00b question
    By Deo in forum Game Programming
    Replies: 2
    Last Post: 05-26-2005, 03:28 PM
  3. help with C++ode(n00b question)
    By staticx57 in forum C++ Programming
    Replies: 8
    Last Post: 10-09-2004, 12:01 AM
  4. n00b question
    By gcn_zelda in forum C++ Programming
    Replies: 4
    Last Post: 03-20-2003, 09:28 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21