-1

I am trying to add command line arguments to my programs. So I was experimenting and cannot figure out this intellisense warning for the life of me. It keeps on saying it is expecting a ')', but I don’t have any idea why.

Here is the code it does not like:

// Calculate average
average = sum / (argc – 1);

Then it underlines the subtraction operator. Below is the full program.

#include <iostream>

int main(int argc, char *argv[])
{
    float average;
    int sum = 0;

    // Valid number of arguments?
    if (argc > 1)
    {
         // Loop through the arguments, ignoring the first which is
         // the name and path of this program
         for (int i = 1; i < argc; i++)
         {
             // Convert cString to int
             sum += atoi(argv[i]);
         }

         // Calculate average
         average = sum / (argc – 1);
         std::cout << "\nSum: " << sum << '\n'
                   << "Average: " << average << std::endl;
    }
    else
    {
        // If an invalid number of arguments, display an error message
        // and usage syntax
        std::cout << "Error: No arguments\n"
                  << "Syntax: command_line [space-delimited numbers]"
                  << std::endl;
    }

    return 0;
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
MrPickle5
  • 522
  • 4
  • 9
  • 31
  • 2
    It might try to warn you that you're probably expecting something different from what you're computing. Tip: What are the types of `sum` and `argc`? :-) – Kerrek SB Feb 17 '13 at 20:49
  • If compiled, this would (most likely) result in 'stray errors', triplet 342, 200, and 223 (octal) (UTF-8 sequence for Unicode code point U+2013 ([EN DASH](https://www.utf8-chartable.de/unicode-utf8-table.pl?start=8192&number=128)).). – Peter Mortensen May 23 '23 at 15:08
  • This is a ***very*** common error when copying code from web pages, [PDF](https://en.wikipedia.org/wiki/Portable_Document_Format) documents, through chat (e.g. [Skype Chat](https://en.wikipedia.org/wiki/Features_of_Skype#Skype_chat) or [Facebook Messenger](https://en.wikipedia.org/wiki/Facebook_Messenger)), etc. The canonical question is *[Compilation error: stray ‘\302’ in program, etc.](https://stackoverflow.com/questions/19198332)*. – Peter Mortensen May 23 '23 at 15:09
  • What compiler and/or IDE? What version? What is the *exact* error message? – Peter Mortensen May 23 '23 at 15:31

2 Answers2

9

The character you think is a minus sign is something else, so it is not parsed as a subtraction operator.

Your version:

average = sum / ( argc – 1 ); 

Correct version (cut and paste into your code):

average = sum / ( argc - 1 ); 

Note that calculating an average using integers might not be the best way to do it. You have integer arithmetic on the RHS, which you then assign to float on the LHS. You should perform the division using floating point types. Example:

#include <iostream>

int main()
{
  std::cout << float((3)/5) << "\n"; // int division to FP: prints 0!
  std::cout << float(3)/5 << "\n";   // FP division: prints 0.6
}
juanchopanza
  • 223,364
  • 34
  • 402
  • 480
2

I tried to compile your code on my machine with g++ 4.6.3 and got the follow error:

cd ~
g++ teste.cpp -o  teste

Output:

teste.cpp:20:8: erro: stray ‘\342’ in program
teste.cpp:20:8: erro: stray ‘\200’ in program
teste.cpp:20:8: erro: stray ‘\223’ in program
teste.cpp: Na função ‘int main(int, char**)’:
teste.cpp:16:33: erro: ‘atoi’ was not declared in this scope
teste.cpp:20:35: erro: expected ‘)’ before numeric constant

Looks like there is some strange char in that line. Remove and re-write the line fixed the error.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Pedro Alves
  • 1,667
  • 4
  • 17
  • 37
  • It isn't mysterious. 342 200 223 (octal) → 0xE2 0x80 0x93 (hexadecimal) → UTF-8 sequence for Unicode code point U+2013 ([EN DASH](https://www.utf8-chartable.de/unicode-utf8-table.pl?start=8192&number=128)). It can be searched (and replaced) for by using the regular expression `\x{2013}` in any modern text editor or IDE. Note: The notation is different in Visual Studio Code (and probably others): `\u2013` (instead of `\x{2013}`) – Peter Mortensen May 23 '23 at 15:01
  • [Clang](https://en.wikipedia.org/wiki/Clang) is less cryptic, but, sadly, less specific: "`error: non-ASCII characters are not allowed outside of literals and identifiers`". (At least by default.) – Peter Mortensen May 23 '23 at 15:19