GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 01-Mar-2004, 11:04
ambeco ambeco is offline
New Member
 
Join Date: Feb 2004
Posts: 23
ambeco is on a distinguished road
Question

Loop problems


Most of the header files that I know how to use did not come with my compiler, so recently I have been programming some functions that do some of the same things. I need the practice anyway. This program was suppost to test a string length function, and function that finds substrings. But my loops are having lots of errors. I am using Borland 5.5 on Win XP, and if anyone can help it would be greatly appreciated. I'm going to post all the code so that you might be able to find the problem better.
CPP / C++ / C Code:
#include <iostream.h>
#include <string.h>

int substring(char *, char *);
void charins(char *, char *, int);
int len(char *);

int main()
{
	char bigstring[50];
	char littlestring[50];
             int place;
	cout << "big phrase\n";
	cin.get(bigstring, 50);
	cin.ignore(50, '\n');
	cout << "little word\n";
	cin.get(littlestring, 50);
	cin.ignore(50, '\n');
	if (substring(bigstring, littlestring))
	{cout << littlestring << " is in " << bigstring << "!!!\n";}
	else
	{cout << littlestring << " isn't in " << bigstring << ".\n";}
	cout << "place\n";
	cin >> place;
	cin.ignore(10, '\n');
	charins(bigstring, littlestring, place);
	return 0;
}

int substring(bigstring, littlestring)
{
	int loc1;
	int loc2;
	int match;
	int placeofsub
	for(loc1 = 1;loc1 == (len(bigstring) - len(littlestring)); loc1++) //six errors here
	{
		match = 1;
		for(loc2 = 1;loc2 == len(littlestring); loc2++)
		{
			if (bigstring[loc1 + loc2] != littlestring[loc2])
			{match = 0;}
		}
		if (match == 1)
		{placeofsub = loc1;}
	}
	return placeofsub;
}

void charins(bigstring, littlestring, place) //lots of errors throughout this subprogram
{
	char firsthalf[50];
	char secondhalf[50];
	int pos1;
	for (pos1 = 1;pos1 +1!= place; pos1++)
	{firsthalf[pos1] = bigstring[pos1];}
	for (pos1 = 1; pos1 + len(firsthalf) != len(bigstring); pos1++)
	{secondhalf[pos1] = bigstring[pos1+len(firsthalf)];}
	strcpy(bigstring, "");
	strcat(bigstring, firsthalf);
	strcat(bigstring, littlestring);
	strcat(bigstring, secondhalf);
}

int len(stringa)
{
	int stringlen; 
	stringlen = 0;
	while (stringa[stringlen] != '\0') // Invalid indirection here
	{stringlen++;}
	stringlen = stringlen - 1;
	return stringlen;
}
I have no idea what any of the errors mean, so please help! I think the functions are trying to use my bigstring/littlestring as integers for some reason...
Last edited by ambeco : 01-Mar-2004 at 11:31. Reason: error with another function and had idea
  #2  
Old 01-Mar-2004, 11:55
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,373
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
Quote:
Originally Posted by ambeco
Most of the header files that I know how to use did not come with my compiler, so recently I have been programming some functions that do some of the same things. I need the practice anyway. This program was suppost to test a string length function, and function that finds substrings. But my loops are having lots of errors. I am using Borland 5.5 on Win XP, and if anyone can help it would be greatly appreciated. I'm going to post all the code so that you might be able to find the problem better.
What header files don't come with 5.5? I use the compiler and all standard headers are there? If you are missing standard headers, download another copy.
Quote:
Originally Posted by ambeco
I have no idea what any of the errors mean, so please help! I think the functions are trying to use my bigstring/littlestring as integers for some reason...
Neither do we since you didn't tell us what the errors were.
Two things I notice:
On the line defining placeofsub you are missing something that will cause many errors,
On the function definition above what are the parameter types?
  #3  
Old 01-Mar-2004, 12:56
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
Hi Ambeco.

This may not be the source of your problem but would definitely help for readability. In addition remember that you can overload functions, so instead of your function headers reading:
CPP / C++ / C Code:
int substring(bigstring, littlestring)

Change them to give the variable types as well:
CPP / C++ / C Code:
int substring(char* bigstring, char* littlestring)

I am not 100% certain, but I believe that C++ will read this as an overload function with integers instead of char*'s
  #4  
Old 01-Mar-2004, 14:59
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,310
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Well, my free version of Borland++ has all of the headers that I have
tried to use so far; where did you get yours?

I ran BCC on your program and got 15 compile errors.

Here are the first few:
Quote:
Borland C++ 5.5.1 for Win32 Copyright (c) 1993, 2000 Borland
s.cpp:
Warning W8054 s.cpp 30: Style of function definition is now obsolete
Error E2141 s.cpp 36: Declaration syntax error in function substring()
Error E2034 s.cpp 36: Cannot convert 'int' to 'char *' in function substring()
Error E2340 s.cpp 36: Type mismatch in parameter 1 (wanted 'char *', got 'int' ) in function substring()
Error E2034 s.cpp 36: Cannot convert 'int' to 'char *' in function substring()
Error E2340 s.cpp 36: Type mismatch in parameter 1 (wanted 'char *', got 'int ' ) in function substring()
Error E2379 s.cpp 36: Statement missing ; in function substring()

Now look at what the compiler is telling you: your line 30 is:
CPP / C++ / C Code:
int substring(bigstring, littlestring)

Since you didn't declare types of the arguments, the default behavior
of C (and, by extension of C++) is to treat them as ints. This leads
to lots of errors inside the subroutine.

change this to something like

CPP / C++ / C Code:
int substring(char bigstring[], char littlestring[])

or
CPP / C++ / C Code:
int substring(char *bigstring, char *littlestring)

Now the compiler knows that they are pointers to char.

Before you recompile with this change, note error #2379:
missing semicolon. Fix it (by adding a semicolon, naturally).

Now, use compiler messages to get a clean compile. That won't
guarantee that your program is correct, but at least you will
have something to look at to see if it's OK

OK?

Dave
  #5  
Old 02-Mar-2004, 10:55
ambeco ambeco is offline
New Member
 
Join Date: Feb 2004
Posts: 23
ambeco is on a distinguished road
Talking

Thanks Guys!


I added the char* to the declarations and added the ; and messed with a few other things, so now it compiles. I will have to add bits of code to prevent errors but... thats a different story. In the future I will remember to include the errors. Sorry about the confusion!
  #6  
Old 02-Mar-2004, 11:36
ambeco ambeco is offline
New Member
 
Join Date: Feb 2004
Posts: 23
ambeco is on a distinguished road
Question

Hmmmm


CPP / C++ / C Code:
int substring(char * bigstring, char * littlestring)
{
	int loc1;
	int loc2;
	int match;
	int placeofsub;
	placeofsub = 0;
	for(loc1 = 1;loc1 == (len(bigstring) - len(littlestring)+1); loc1++)
	{
		match = 1;
		for(loc2 = 1;loc2 == len(littlestring); loc2++)
		{
			if (bigstring[loc1 + loc2-1] != littlestring[loc2])
			{match = 0;}
		}
		if (match == 1)
		{placeofsub = loc1;}
	}
	return placeofsub;
}
This all compiles correctly, but always says the small string isn't in the big string (returns 0) it should return the digit of the last place it was found. Anyone know why?
  #7  
Old 02-Mar-2004, 11:48
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
Your for statements are never being processed. You are telling your loop statement to process only when loc1 is equal to your string length which is never, because your loc1 doesn't get indexed. Use:

CPP / C++ / C Code:
for(loc2 = 1;loc2 <= len(littlestring); loc2++)

in both of your for statements.
  #8  
Old 02-Mar-2004, 12:13
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,310
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
When I have programs with several new routines and the big program
doesn't give expected result, sometimes I test the individual routines
in separate programs.

For example: I think that your len() function gives the wrong answer.
(It returns a result that is 1 more than the actual string length).

Try the following:

CPP / C++ / C Code:
/* test of len() function to obtain length of a C string */
#include <stdio.h>

int main()
{

  int len(char *);

  char *s1 = "This is the first string";
  char *s2 = "Second";
  char *s3 = "";

  printf("Length of <%s> is %d\n", s1, len(s1));
  printf("Length of <%s> is %d\n", s2, len(s2));
  printf("Length of <%s> is %d\n", s3, len(s3));

  return 0;

}


int len(char *stringa)
{

  int stringlen; 

  stringlen = 0;

  while (stringa[stringlen] != '\0') {
    stringlen++;
  }
  /* stringlen = stringlen - 1; */ /* why did you do this? */
  return stringlen;

}

The first string has length = 24
The second has length = 6
The third has length = 0

(Note that your test cases should always include boundary
conditions --- i.e. make sure it's OK for all strings, including an empty
string.)

Now if you have full confidence in this function, you can concentrate
on any remaining problems.

Regards,

Dave
  #9  
Old 03-Mar-2004, 10:02
ambeco ambeco is offline
New Member
 
Join Date: Feb 2004
Posts: 23
ambeco is on a distinguished road
Lightbulb

Quote:
Originally Posted by dsmith
Your for statements are never being processed. You are telling your loop statement to process only when loc1 is equal to your string length which is never, because your loc1 doesn't get indexed.
Oh right, sorry. Im still getting used to C++ from QBASIC whereyou tell it when to stop instead of when to continue. And from now on, im going to make these test programs simpler, because this WAS my simple test program. Thanks guys!
 
 

Recent GIDBlogInstall Adobe Flash - Without Administrator Rights by LocalTech

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
burning problems PLEASE PLEASE HELP kelticeire Computer Hardware Forum 4 01-Dec-2006 15:39
Chaintech Geforce 5600 FX problems bartster74 Computer Hardware Forum 8 04-May-2004 13:16
C switch / loop issue spudtheimpaler C Programming Language 4 20-Feb-2004 20:45
CD Burner Problems Joe9995 Computer Hardware Forum 0 07-Feb-2004 10:47
Installation problems arman Apache Web Server Forum 3 03-Feb-2004 12:02

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 15:44.


vBulletin, Copyright © 2000 - 2010, Jelsoft Enterprises Ltd.