Jump to content
  • Advertisement
Sign in to follow this  
nectron101

errors I can't fix..

This topic is 4856 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have the following code.. It's supposed to sort the strings in the array in order.. I wrote the program, and it worked well.. After I corrected somethings.. other errors showed up.. What am I doing wrong?
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#define MAXLINES 25

void swapsp();
void sort(char *p[], int n);
void print_strings(char *p[], int n);

main()
{
 int numwords = 30;
 char *words[30]={      "computer", "hard", "soft", "clear", "message",
                        "button", "compact", "laser", "mouse", "speaker",
                        "error", "disk", "simple", "complex", "inside",
                        "watch", "bay", "already", "pixel", "nano",
                        "binary", "hex", "decimal", "java", "light",
                        "mega", "giga", "water", "caffiene", "camel"};
 sort(words, numwords);
 print_strings(words, numwords);
 system ("PAUSE");
 return(0);
}



void sort(char *p[], int n)
{
 int a, b;
 char *x;
 for (a = 1; a < n; a++)
 {
   for (b = 0; b < n-1; b++)
   {
    swapsp();
   }
 }
}

void print_strings(char *p[], int n)
{
 int counter;
 for (counter = 0; counter < n; counter++)
 printf("%s\n", p[counter]);
}

void swapsp()
{
     if (strcmp(p, p[b+1]) > 0)      //strcmp to compare strings
     {
      x = parray;
      p = p[b+1];
      p[b+1] = x;
      }
}
Thanks in advance..

Share this post


Link to post
Share on other sites
Advertisement
It's been only fifteen minutes and you didn't bother to say what error you were getting. I think you have an unreasonable expectation for turn around time here.

Share this post


Link to post
Share on other sites

void swapsp()
{
if (strcmp(p, p[b+1]) > 0) //strcmp to compare strings
{
x = parray;
p = p[b+1];
p[b+1] = x;
}
}




i dont see where any of these variables are declared/defined. you probably want them as arguments to your function...

i fail to see how this code worked before, unless you deleted some stuff...

Share this post


Link to post
Share on other sites
Sorry..
We're all programmers, and we all forget..

Here're the errors that I got..
I tried many different things, but it just doesn't work...


53 e:\cprogh~1\hw1\1.3\webspme.cpp
`p' undeclared (first use this function)

53 e:\cprogh~1\hw1\1.3\webspme.cpp
`b' undeclared (first use this function)

55 e:\cprogh~1\hw1\1.3\webspme.cpp
`x' undeclared (first use this function)

Share this post


Link to post
Share on other sites
You need to pass in the arguments to your function swapsp()


void sort(char *p[], int n)
{
int a, b;
char *x;
for (a = 1; a &lt; n; a++)
{
for (b = 0; b &lt; n-1; b++)
{
swapsp(); // Your swapsp funtion uses p and b, which are
// currently unreachable.
// Why not just use the strcmp, in this for() ?
}
}
}



Daed.

Share this post


Link to post
Share on other sites
Damn!!

it's even messier now.. I think I should take a break and look at it later.. this happens a lot..


for (b = 0; b < n-1; b++)
{
swapsp(p, b, x);
}
}
}

void print_strings(char *p[], int n)
{
int counter;
for (counter = 0; counter < n; counter++)
printf("%s\n", p[counter]);
}

void swapsp(int p, int b, int x)
{
if (strcmp(p, p[b+1]) > 0) //strcmp to compare strings
{
x = p;
p = p[b+1];
p[b+1] = x;
}
}

Share this post


Link to post
Share on other sites
At a glance, I see that you need a return type on your main function:

int main()
{
...
}

I've never seen/used system("PAUSE"), so unless this is defined somewhere else, I think this is probably an error.

The variable b needs to be an input parameter for swapsp. This variable is local to sort, but swapsp does not know about it. Same goes for char* x. parray is not defined; I think you intended to use "p".
You also need to define char* x in the swapsp() function. For now, you should just merge swapsp into the sort().

[SOURCE]
void sort(char *p[], int n)
{
int a, b;
char *x;
for (a = 1; a < n; a++)
{
for (b = 0; b < n-1; b++)

{
if (strcmp(p, p[b+1]) > 0) //strcmp to compare strings
{
x = p;
p = p[b+1];
p[b+1] = x;
}
}
}
}
[/SOURCE]


Once this is up and running, you are going to get a runtime error, because you are accessing an array out of bounds. At the end of the inner loop, b will have the value of 29, and b+1 will equal 30.

Share this post


Link to post
Share on other sites
Quote:
Original post by nectron101
Damn!!

it's even messier now.. I think I should take a break and look at it later.. this happens a lot..


for (b = 0; b < n-1; b++)
{
swapsp(p, b, x);
}
}
}

void print_strings(char *p[], int n)
{
int counter;
for (counter = 0; counter < n; counter++)
printf("%s\n", p[counter]);
}

void swapsp(int p, int b, int x)
{
if (strcmp(p, p[b+1]) > 0) //strcmp to compare strings
{
x = p;
p = p[b+1];
p[b+1] = x;
}
}


p is probably going to be char **

and x is a temporary char *, not a argument

Share this post


Link to post
Share on other sites
Ok..

Here's what I have now, but there are still errors..
39 e:\cprogh~1\hw1\1.3\webspme.cpp
parse error before `]'

54 e:\cprogh~1\hw1\1.3\webspme.cpp
passing `char' to argument 1 of `strcmp(const char *, const char *)' lacks a cast

54 e:\cprogh~1\hw1\1.3\webspme.cpp
passing `char' to argument 2 of `strcmp(const char *, const char *)' lacks a cast



#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#define MAXLINES 25

void swapsp();
void sort(char *p[], int n);
void print_strings(char *p[], int n);

main()
{
int numwords = 30;
char *words[30]={ "computer", "hard", "soft", "clear", "message",
"button", "compact", "laser", "mouse", "speaker",
"error", "disk", "simple", "complex", "inside",
"watch", "bay", "already", "pixel", "nano",
"binary", "hex", "decimal", "java", "light",
"mega", "giga", "water", "caffiene", "camel"};
sort(words, numwords);
print_strings(words, numwords);
system ("PAUSE");
return(0);
}



void sort(char *p[], int n)
{
int a, b;
char *x;
for (a = 1; a < n; a++)
{
for (b = 0; b < n-1; b++)
{
swapsp(p[], b);
}
}
}

void print_strings(char *p[], int n)
{
int counter;
for (counter = 0; counter < n; counter++)
printf("%s\n", p[counter]);
}

void swapsp(char p[], int b)
{
int x;
if (strcmp(p, p[b+1]) > 0) //strcmp to compare strings
{
x = p;
p = p[b+1];
p[b+1] = x;
}
}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!