Anything wrong with this c++ code?

I don't know what i'm doing wrong. I did everything perfectly and it's showing massive amounts of syntax errors according to my professor. I have checked over and over and see no errors. I have had friends check and see no problems with it. The only problem I see is when compiling. Anybody know whats up with this?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/* File Name: Rawr
 * Author: Rawr
 * Date: 10/30/2013
*/ Description:random number

#include<iostream>
using namespace std;
int linearSearch(int[], int, int);
int main()
{
        int Size, SearchNum;
        int arr[5]={1,2,3,4,5};
        SearchNum=3;
        Size=5;

        int foundIndex=linearSearch(arr[], Size, SearchNum);
        if (foundIndex==-1)
        {
                cout << "Not found\n";
        }        
        else
        {        
                cout <<  "Found: " << SearchNum << "at index: " << foundIndex << "\n";
        }          
}


/* Function: linearSearch
 * Inputs: arr, Size, and SearchNum
 * Outputs: Found or Not Found
*/
int linearsearch(int arr[], int Size, int SearchNum)
{
	for(int i=0; i < Size; i++)
	{
		if(SearchNum==arr[i])
		{
			return(i);
		}
	}
	return(-1);
}
	
Last edited on
Look very carefully at the syntax highlighting on line 4
And then carefully compare the name of the function you declare on line 8 (and use on line 16) to the one you define on line 32.

Then get rid of the square brackets on line 16.
Last edited on
Branflakes91093 wrote:
Then get rid of the square brackets on line 16.
I think that is actually legal syntax for some compilers are a non-standard extension. Obviously it shouldn't be used...
The only problem I see is when compiling.


That's a pretty big problem if your code can't be compiled, isn't it? Paying attention to the error messages at compilation is the first step towards self sufficiency.

If you ever find yourself uttering the words "I did everything perfectly" followed by "the only problem I see is when compiling" reconsider the former.
Two errors

1 : int foundIndex = linearSearch(arr, Size, SearchNum);

change arr[] to arr

2 : int linearSearch(int arr[], int Size, int SearchNum)

change linearsearch to linearSearch

try to understand the error messages of the compiler give you.

Bonus :

1 : c++ is not c89, you don't need to declare every parameters before the function, please study what is "lazy initialization".

2 : please study stl, it is an essential part of c++.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <algorithm>
#include <array>
#include <iostream>
#include <iterator>

int main()
{
    std::array<int, 5> arr{{1,2,3,4,5}};
    //or you could use int arr[] = {1,2,3,4,5}; 
    auto it = std::find(std::begin(arr), std::end(arr), 3);
    if(it != std::end(arr)){
       //you could use std::distance to find the distance too, it is more generic   
        std::cout<<*it<<" found at index "<<(std::end(arr) - it)<<std::endl;
    }
}


3 : to write robust, efficient c++ codes, we only need to know the contents of ch1~ch16 of c++ primer 5(I am still studying edition 5)
Last edited on
closed account (N36fSL3A)
Well usually professors want students to follow what they teach them, and sometimes that doesn't include using the STL.
It is important to know what the correct way to do something is alongside what your professor wants you to do.
closed account (N36fSL3A)
I wholly agree with you there LB. I've never actually took a college class, but the few solutions I've given to others problems were turned down because of picky profs.
Last edited on
Topic archived. No new replies allowed.