1,105,242 Community Members

Quick Question about my code

Member Avatar
Jay22
Newbie Poster
5 posts since Feb 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

ok so basically my code is complete but I keep getting an error on one of my brackets and it says I need to put a ' ; ' behind "int LinearSearchArray ( const int list[], int numEless, int value)" and when I do that it brings up more errors. Besides the bracket error my code is complete. The error is at the bottom of my code and I made a comment with a arrow pointing to the error. Thanks

#include <string>
#include <iostream>
#include <ctime>
#include <cmath>
#include <fstream>

using namespace std;

int main()
{
 
int linearSearchArray(const int[], int, int);


 
int vals[100];
fstream file;



 
string line =   "----------------------------------------------------\n";
       string msg2 =   " Please select an option from the above menu --> ";
       string menu1 =  " [0]    Quit \n"
         " [1]    Generate Rand Array \n"
         " [2]    Save Array to File \n"
         " [3]    Load Array from File \n"
         " [4]    Search Array \n"
         " [5]    Compute CPU Time \n";

int sz;
int max_range= 100;
int min_range= 0;
ofstream fout;
ifstream fin;
int idx;
int val;
long t0, t1;
int op = -1;
 
while( op != 0)
{
cout << menu1;
cout << msg2 << endl;
cin >> op;
cout << line << endl;
 
 
switch( op )
{

case 0: exit(0); break;

case 1:
int sz;
cout << " Please enter size of the array (sz <= 100) --> ";
cin >> sz;

if(sz > 1000){ //MAX_SZ
cout << " The size you entered is not allowed ...\n";
exit(0);
}
else{
int max_range = 100;
int min_range = 0;
for( int i=0; i<sz; ++i){
vals[i] = min_range +  rand() % max_range;
 
}
}
break;

case 2: 

 
float n1, n2;
 
  fout.open("fout.txt");

  for( int i=0; i<sz; ++i)
  {
 fout << vals[i] << endl;
  }

  fout.close();
  break;
 
  

  
 
case 3:
fin.open("fout.txt");
idx = 0;
while(fin){
fin >> vals[idx];
++idx;
}

sz = idx;
fin.close();
break;
case 4:
cout << " Please enter the value you are looking for --->";
cin >> val;
idx = linearSearchArray(vals, sz, val);
if( idx > -1)
cout << " The val (" << val << ") was found at index (" << idx << ") \n\n";
else 
cout << " The Val (" << val << ") was not found \n\n" ;

cout << "CPU time was" << t1-t0 << "ms\n\n"; 
 
 
case 5:
long t0 = clock(); //CPU Time
 
for(float i=0; i<100000; ++i)  
pow(i,i) * sqrt(i*i) / exp(i);
 
long t1 = clock();
 
cout << "running time in ms = " << t1-t0 << "\n";

case 6:
for(int i=0; i<sz; ++i)
{
cout <<  vals[i] << endl;
}
break;

default: cout << "Invalid option...Please try again \n\n";
}
return 0;
}
 
int LinearSearchArray ( const int list[], int numEless, int value)
 
{                         //<<--------this is where I get my error 
int index=0;
int position= -1;
bool found = false;


while(index < numEless &&!found)
{
found = true;
position = index;
}

index ++
}
return position
}
Member Avatar
WaltP
Posting Sage w/ dash of thyme
9,363 posts since May 2006
Reputation Points: 2,905 [?]
Q&As Helped to Solve: 1,151 [?]
Skill Endorsements: 45 [?]
Team Colleague
 
0
 

1) USE CODE TAGS!
2) Format your code.
Almost all errors like this can be avoided by proper formatting.
3) Never tell us what the error says, post it verbatim (copy/paste)

Member Avatar
DJSAN10
Posting Whiz in Training
260 posts since Dec 2010
Reputation Points: 28 [?]
Q&As Helped to Solve: 27 [?]
Skill Endorsements: 0 [?]
 
1
 

int LinearSearchArray ( const int list[], int numEless, int value)

{ //<<--------this is where I get my error
int index=0;
int position= -1;
bool found = false;


while(index < numEless &&!found)
{
found = true;
position = index;
}

index ++
}
return position
}

There is no semicolon after last two lines
index++ and return position

Member Avatar
MandrewP
Junior Poster
151 posts since Nov 2009
Reputation Points: 60 [?]
Q&As Helped to Solve: 26 [?]
Skill Endorsements: 1 [?]
 
0
 

ok so basically my code is complete but I keep getting an error on one of my brackets and it says I need to put a ' ; ' behind "int LinearSearchArray ( const int list[], int numEless, int value)" and when I do that it brings up more errors. Besides the bracket error my code is complete. The error is at the bottom of my code and I made a comment with a arrow pointing to the error. Thanks

#include <string>
#include <iostream>
#include <ctime>
#include <cmath>
#include <fstream>

using namespace std;

int main()
{
 
int linearSearchArray(const int[], int, int);


 
int vals[100];
fstream file;



 
string line =   "----------------------------------------------------\n";
       string msg2 =   " Please select an option from the above menu --> ";
       string menu1 =  " [0]    Quit \n"
         " [1]    Generate Rand Array \n"
         " [2]    Save Array to File \n"
         " [3]    Load Array from File \n"
         " [4]    Search Array \n"
         " [5]    Compute CPU Time \n";

int sz;
int max_range= 100;
int min_range= 0;
ofstream fout;
ifstream fin;
int idx;
int val;
long t0, t1;
int op = -1;
 
while( op != 0)
{
cout << menu1;
cout << msg2 << endl;
cin >> op;
cout << line << endl;
 
 
switch( op )
{

case 0: exit(0); break;

case 1:
int sz;
cout << " Please enter size of the array (sz <= 100) --> ";
cin >> sz;

if(sz > 1000){ //MAX_SZ
cout << " The size you entered is not allowed ...\n";
exit(0);
}
else{
int max_range = 100;
int min_range = 0;
for( int i=0; i<sz; ++i){
vals[i] = min_range +  rand() % max_range;
 
}
}
break;

case 2: 

 
float n1, n2;
 
  fout.open("fout.txt");

  for( int i=0; i<sz; ++i)
  {
 fout << vals[i] << endl;
  }

  fout.close();
  break;
 
  

  
 
case 3:
fin.open("fout.txt");
idx = 0;
while(fin){
fin >> vals[idx];
++idx;
}

sz = idx;
fin.close();
break;
case 4:
cout << " Please enter the value you are looking for --->";
cin >> val;
idx = linearSearchArray(vals, sz, val);
if( idx > -1)
cout << " The val (" << val << ") was found at index (" << idx << ") \n\n";
else 
cout << " The Val (" << val << ") was not found \n\n" ;

cout << "CPU time was" << t1-t0 << "ms\n\n"; 
 
 
case 5:
long t0 = clock(); //CPU Time
 
for(float i=0; i<100000; ++i)  
pow(i,i) * sqrt(i*i) / exp(i);
 
long t1 = clock();
 
cout << "running time in ms = " << t1-t0 << "\n";

case 6:
for(int i=0; i<sz; ++i)
{
cout <<  vals[i] << endl;
}
break;

default: cout << "Invalid option...Please try again \n\n";
}
return 0;
}
 
int LinearSearchArray ( const int list[], int numEless, int value)
 
{                         //<<--------this is where I get my error 
int index=0;
int position= -1;
bool found = false;


while(index < numEless &&!found)
{
found = true;
position = index;
}

index ++
}
return position
}

Your "LinearSearchArray" function is defined within main() - it needs to be defined globally. Also, you have 5 brackets in this function, so you either have to add one more or remove one. Having an odd number of brackets is never a good thing!

Member Avatar
MandrewP
Junior Poster
151 posts since Nov 2009
Reputation Points: 60 [?]
Q&As Helped to Solve: 26 [?]
Skill Endorsements: 1 [?]
 
0
 

ok so basically my code is complete but I keep getting an error on one of my brackets and it says I need to put a ' ; ' behind "int LinearSearchArray ( const int list[], int numEless, int value)" and when I do that it brings up more errors. Besides the bracket error my code is complete. The error is at the bottom of my code and I made a comment with a arrow pointing to the error. Thanks

#include <string>
#include <iostream>
#include <ctime>
#include <cmath>
#include <fstream>

using namespace std;

int main()
{
 
int linearSearchArray(const int[], int, int);


 
int vals[100];
fstream file;



 
string line =   "----------------------------------------------------\n";
       string msg2 =   " Please select an option from the above menu --> ";
       string menu1 =  " [0]    Quit \n"
         " [1]    Generate Rand Array \n"
         " [2]    Save Array to File \n"
         " [3]    Load Array from File \n"
         " [4]    Search Array \n"
         " [5]    Compute CPU Time \n";

int sz;
int max_range= 100;
int min_range= 0;
ofstream fout;
ifstream fin;
int idx;
int val;
long t0, t1;
int op = -1;
 
while( op != 0)
{
cout << menu1;
cout << msg2 << endl;
cin >> op;
cout << line << endl;
 
 
switch( op )
{

case 0: exit(0); break;

case 1:
int sz;
cout << " Please enter size of the array (sz <= 100) --> ";
cin >> sz;

if(sz > 1000){ //MAX_SZ
cout << " The size you entered is not allowed ...\n";
exit(0);
}
else{
int max_range = 100;
int min_range = 0;
for( int i=0; i<sz; ++i){
vals[i] = min_range +  rand() % max_range;
 
}
}
break;

case 2: 

 
float n1, n2;
 
  fout.open("fout.txt");

  for( int i=0; i<sz; ++i)
  {
 fout << vals[i] << endl;
  }

  fout.close();
  break;
 
  

  
 
case 3:
fin.open("fout.txt");
idx = 0;
while(fin){
fin >> vals[idx];
++idx;
}

sz = idx;
fin.close();
break;
case 4:
cout << " Please enter the value you are looking for --->";
cin >> val;
idx = linearSearchArray(vals, sz, val);
if( idx > -1)
cout << " The val (" << val << ") was found at index (" << idx << ") \n\n";
else 
cout << " The Val (" << val << ") was not found \n\n" ;

cout << "CPU time was" << t1-t0 << "ms\n\n"; 
 
 
case 5:
long t0 = clock(); //CPU Time
 
for(float i=0; i<100000; ++i)  
pow(i,i) * sqrt(i*i) / exp(i);
 
long t1 = clock();
 
cout << "running time in ms = " << t1-t0 << "\n";

case 6:
for(int i=0; i<sz; ++i)
{
cout <<  vals[i] << endl;
}
break;

default: cout << "Invalid option...Please try again \n\n";
}
return 0;
}
 
int LinearSearchArray ( const int list[], int numEless, int value)
 
{                         //<<--------this is where I get my error 
int index=0;
int position= -1;
bool found = false;


while(index < numEless &&!found)
{
found = true;
position = index;
}

index ++
}
return position
}

Ooops - sorry, your function is not defined within main(). However, you do have 13 brackets in main(), so again, you need to add one more or remove one.

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: