Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,120 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,773 people online right now. Registration is fast and FREE... Join Now!




what does the error mean? how can i fix my code?

 
Reply to this topicStart new topic

what does the error mean? how can i fix my code?

shahnozajon
17 Apr, 2007 - 09:10 AM
Post #1

New D.I.C Head
*

Joined: 9 Apr, 2007
Posts: 13


My Contributions
CODE
#include <iostream>
#include <cstdlib>
using namespace std;

int binarySearch (int searchValue, const int a[], int n)
{
    int low;
    int high;
    int mid;
    low = 0;
    high = n - 1;
    while (low <= high)
    {
          mid = (low + high) / 2;
          if (searchValue == a[mid])
              return mid;
          else if (searchValue < a[mid])
              high = mid - 1;
          else
              low = mid + 1;
    }
    return -1;
}



my error says: [Linker Error] undefined reference to 'WinMain@16'
what is wrong with the code?
User is offlineProfile CardPM
+Quote Post

Amadeus
RE: What Does The Error Mean? How Can I Fix My Code?
17 Apr, 2007 - 09:12 AM
Post #2

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,226



Thanked: 37 times
Dream Kudos: 25
My Contributions
You don't have a main function the program can use as an entry point.
User is offlineProfile CardPM
+Quote Post

shahnozajon
RE: What Does The Error Mean? How Can I Fix My Code?
17 Apr, 2007 - 09:23 AM
Post #3

New D.I.C Head
*

Joined: 9 Apr, 2007
Posts: 13


My Contributions
QUOTE(Amadeus @ 17 Apr, 2007 - 10:12 AM) *

You don't have a main function the program can use as an entry point.


CODE

#include <iostream>
#include <cstdlib>
using namespace std;

int main {}
{
    int low;
    //int high;
    int mid;
    int n;
    int low = 0;
    int high = n - 1;
}

int binarySearch (int searchValue, const int a[], int n)
{
    while (low <= high)
    {
          mid = (low + high) / 2;
          if (searchValue == a[mid])
              return mid;
          else if (searchValue < a[mid])
              high = mid - 1;
          else
              low = mid + 1;
    }
    return -1;
}


i added the main function. i dunno if i completely messed it up but now it's giving me a parse error and a syntax error.
User is offlineProfile CardPM
+Quote Post

Amadeus
RE: What Does The Error Mean? How Can I Fix My Code?
17 Apr, 2007 - 09:36 AM
Post #4

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,226



Thanked: 37 times
Dream Kudos: 25
My Contributions
Fixed syntax errors only (with a few notes on other matters):
CODE

#include <iostream>
#include <cstdlib>
using namespace std;

int binarySearch (int searchValue, const int a[], int n);

int main()
{
    int mid;
    int n;
    int low = 0;
    int high = n - 1;//n does not have a value yet...this will create an error
    return 0;
}

int binarySearch (int searchValue, const int a[], int n)
{
    while (low <= high)//this function cannot see mid, low, or high unless they are declared in it's scope, or are global, or are passed to it.
    {
          mid = (low + high) / 2;
          if (searchValue == a[mid])
              return mid;
          else if (searchValue < a[mid])
              high = mid - 1;
          else
              low = mid + 1;
    }
    return -1;//you probably want to return mid here.
}

User is offlineProfile CardPM
+Quote Post

shahnozajon
RE: What Does The Error Mean? How Can I Fix My Code?
17 Apr, 2007 - 10:21 AM
Post #5

New D.I.C Head
*

Joined: 9 Apr, 2007
Posts: 13


My Contributions
QUOTE(Amadeus @ 17 Apr, 2007 - 10:36 AM) *

Fixed syntax errors only (with a few notes on other matters):
CODE

#include <iostream>
#include <cstdlib>
using namespace std;

int binarySearch (int searchValue, const int a[], int n);

int main()
{
    int mid;
    int n;
    int low = 0;
    int high = n - 1;//n does not have a value yet...this will create an error
    return 0;
}

int binarySearch (int searchValue, const int a[], int n)
{
    while (low <= high)//this function cannot see mid, low, or high unless they are declared in it's scope, or are global, or are passed to it.
    {
          mid = (low + high) / 2;
          if (searchValue == a[mid])
              return mid;
          else if (searchValue < a[mid])
              high = mid - 1;
          else
              low = mid + 1;
    }
    return -1;//you probably want to return mid here.
}



thanx for the help!
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/1/08 10:00PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month