| | |
need some help with C string
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
I just reglanced at your first post and you have the same (no room for terminator) problem as well.
You need to add 1 to the buffer length to store the terminator!
C++ Syntax (Toggle Plain Text)
char mycstring[number + 1]; mycstring[number]='\0';
You need to add 1 to the buffer length to store the terminator!
•
•
Join Date: Jun 2009
Posts: 282
Reputation:
Solved Threads: 1
most recent code
//main
//interface
//implementation
#endif // STREXTRA_H_INCLUDED
//main
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <cstdlib> using namespace std; #include "strextra.h" int main() { Search a; cout<<"enter max number of characters in the sentence"; int number; cin>>number; cout<<"enter a sentence"; char let; char mycstring[number+1]; for(int i=0; i<number; i++) { cin>>let; mycstring[i]=let; } mycstring[number+1]='\0'; cout<<"my string is\n"<<mycstring<<"\n"; cout<<"enter the character you are trying to find in the string above\n"; char alpha; char lett[2]; for(int a=0; a<1; a++) { cin>>alpha; lett[a]=alpha; } lett[1]='\0'; cout<<lett[0]<<lett[1]; //a.find(mycstring, lett); }
//interface
C++ Syntax (Toggle Plain Text)
#ifndef STREXTRA_H_INCLUDED #define STREXTRA_H_INCLUDED #include <iostream> using namespace std; class Search { public: int find(char mystring,char letter); int find(char mystring,string word); };
//implementation
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <cstdlib> #include <cstring> using namespace std; #include "strextra.h" int search::find(char mystring,char letter) { return strchr(mystring,letter); }
#endif // STREXTRA_H_INCLUDED
You've almost have your +1 covered but I believe you have a problem.
char mycstring[number+1];
Firstly Your buffer is {0...number-1, number }
Where as number index is the terminator.
Incorrect:
mycstring[ number+1]='\0';
Correct:
mycstring[ number ]='\0';
Secondly the array index requires a constant. Not a dynamic value. That buffer is declared in the scope of the function call. that is it exists on the stack as
FUNCTION RETURN
Local Stack Arguments
Other FUNCTIONS RETURNS when called within the braces {}.
So you either have to dynamically allocte memory, or use an local buffer large enough to contain your largest string. And keep a count of how many characters you've stuffed into it.
char mycstring[number+1];
Firstly Your buffer is {0...number-1, number }
Where as number index is the terminator.
Incorrect:
mycstring[ number+1]='\0';
Correct:
mycstring[ number ]='\0';
Secondly the array index requires a constant. Not a dynamic value. That buffer is declared in the scope of the function call. that is it exists on the stack as
FUNCTION RETURN
Local Stack Arguments
Other FUNCTIONS RETURNS when called within the braces {}.
So you either have to dynamically allocte memory, or use an local buffer large enough to contain your largest string. And keep a count of how many characters you've stuffed into it.
Also you're still using a second loop for the single character entry.
You don't need the loop. Or is this a setup for testing your other function which finds substrings within strings?
Also you have two prototypes for find.
One is ASCIIz string and character, but you're passing ASCIIz string, ASCIIz string.
You don't need the loop. Or is this a setup for testing your other function which finds substrings within strings?
Also you have two prototypes for find.
One is ASCIIz string and character, but you're passing ASCIIz string, ASCIIz string.
•
•
Join Date: Jan 2008
Posts: 3,829
Reputation:
Solved Threads: 501
cout<<"enter max number of characters in the sentence";
int number;
cin>>number;
cout<<"enter a sentence";
char let;
char mycstring[number+1];This is actually compiling for me and I'm a little surprised, since I was taught to never do this. Aren't you either supposed to make
number a constant or allocate the array with the new command? Why isn't this code giving me an error?Looks like wildgoose has the same idea regarding dynamic allocation, but again, shouldn't this not compile at all?
[EDIT]
This is one fast moving thread. 20 posts in less than an hour!
[/EDIT]
Last edited by VernonDozier; Jun 29th, 2009 at 3:55 pm.
![]() |
Similar Threads
Other Threads in the C++ Forum
- Previous Thread: I need help with the errors I developed with C++
- Next Thread: fatal error LNK1120
| Thread Tools | Search this Thread |
api application array arrays based beginner binary c++ c/c++ calculator char char* class classes code compile compiler console conversion count delete deploy desktop directshow dll download dynamic dynamiccharacterarray encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int java lib library linkedlist linker list loop looping loops map math matrix memory newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg simple sorting string strings studio temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






