15,944,136 members
See more:
I was coding to find the missing number:
Given an array nums containing n distinct numbers in the range [0, n], return the only number in the range that is missing from the array.
C++
```main.cpp: In member function ‘int Solution::missingNumber(std::vector<int>&)’:
main.cpp:22:11: error: expected unqualified-id before numeric constant
22 |   for(int 0;i<n;i++){
|           ^
main.cpp:22:10: error: expected ‘;’ before numeric constant
22 |   for(int 0;i<n;i++){
|          ^~
|          ;
main.cpp:22:13: error: ‘i’ was not declared in this scope
22 |   for(int 0;i<n;i++){
|             ^
main.cpp:22:16: error: expected ‘)’ before ‘;’ token
22 |   for(int 0;i<n;i++){
|      ~         ^
|                )
main.cpp:22:17: error: ‘i’ was not declared in this scope
22 |   for(int 0;i<n;i++){
|                 ^
main.cpp:25:2: warning: no return statement in function returning non-void [-Wreturn-type]
25 |  }
|  ^
C```

What I have tried:

This is my code to find the missing number. It gives me error at `for` loop:
C++
```class Solution {
public:
int missingNumber(vector<int>& nums) {
// Your code goes here
int n=nums.size();
int expected_sum=(n*(n+1))/2;
int current_sum=0;
for(int 0;i<n;i++){
current_sum+=nums[i];
}
}
};```
Posted
Updated 12-Sep-23 11:27am
v4

Solution 1

First thing that comes up is

C++
```for(int 0;i<n;i++){
current_sum+=nums[i];```

In the for loop you need to define the variable i;

C++
```for(int i=0;i<n;i++){
current_sum+=nums[i];```

v2

Solution 2

Just to add to what Mike has said, you should expect to get syntax errors every day, probably many times a day while you are coding - we all do regardless of how much experience we have! Sometimes, we misspell a variable, or a keyword; sometimes we forget to close a string or a code block. Sometimes the cat walks over your keyboard and types something really weird. Sometimes we just forget how many parameters a method call needs.

We all make mistakes.

And because we all do it, we all have to fix syntax errors - and it's a lot quicker to learn how and fix them yourself than to wait for someone else to fix them for you! So invest a little time in learning how to read error messages, and how to interpret your code as written in the light of what the compiler is telling you is wrong - it really is trying to be helpful!

So read this: How to Write Code to Solve a Problem, A Beginner's Guide Part 2: Syntax Errors[^] - it should help you next time you get a compilation error!

And spending a little time learning to understand syntax error messages will save you a huge amount of time in future: you waited at least 12 minutes for Mike to reply, then your email system probably added another 10 minutes or so, plus the time it took you to type up the question. Chances are that you could have saved a significant chunk of that time if you knew how to read them!

I'm not saying we don't want to help you fix them - sometimes I can't see my own errors because I read what I meant to write - but fixing syntax errors is part of the job, and if you can't do it for yourself people are going to look at you as a bit weird should you get a job in the industry!

Solution 3

Mike pointed out the primary error. Another one is, as the last message pointed out, the method is declared as returning an integer but it does not have a return statement.

Solution 4

The `missingNumber` method should return the difference between
• the sum of the numbers in the `[0,n]` range.
• The sum of the passed vector items.
The former is correctly written in your code as `n*(n+1)/2`.
You might compute the latter in a loop or, alternatively, use std::accumulate[^].

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Top Experts
Last 24hrsThis month
 Pete O'Hanlon 60 aisha.offical 24 OriginalGriff 10 Stephen Hewison 5 Iman Shabanzade -6
 Pete O'Hanlon 965 OriginalGriff 810 Richard Deeming 715 merano99 345 Dave Kreskowiak 310

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900