|
 |
|
 |
07-25-2005, 06:06 PM
|
#1 (permalink)
|
|
C++ Beginner
Join Date: Jul 2005
Location: Ottawa
Posts: 73
|
New to C++
Hey guys, how's it going. I just started to read a Visual C++ book a few days ago, but I'm not finding it helpful as i'm not getting any programming experience in the book. i've been working with computers for a few years now, and am fairly competent but I need some help as to be pointed into the right direction as to how I should go about starting programmning and just learning on a trial and error basis, as in with which topics I should start off with the most. I touched a bit of console C++ a few years back but don't really recall much of it. Any suggestions would be appreciated.
|
|
|
07-25-2005, 09:21 PM
|
#2 (permalink)
|
|
[code][/code] enforcer
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
|
Then buy a primer on C++ and go from there. Search this site for advise on books.
__________________
|
|
|
07-26-2005, 11:04 AM
|
#3 (permalink)
|
|
C++ Beginner
Join Date: Jul 2005
Location: Ottawa
Posts: 73
|
Ok. I started using teach yourself visual C++ in 21 days, alongside a C++ Primer book from 98 I think. It's going ok i guess, I kind of understand functions now, could anyone give me a simple couple of programs so i can see if I can code something without using references as much as possible?
|
|
|
07-26-2005, 11:41 AM
|
#4 (permalink)
|
|
Newbie
Join Date: Jun 2002
Location: Denmark
Posts: 1,721
|
I'll mention this thread especialy pointing out this quest but with a slight twist, have it read into a std::string and use the std::string c_str() function to convert it into an array, then loop through that in order to remove the word in question.
Here is a layout:
Code:
#include <iostream>
#include <stdlib>
#include <string>
int remove_string(char* in_str, std::string remove_str);
int main()
{
std::string in_str;
std::string remove_str;
char* char_str;
std::cout << "Input string to process: ";
std::cout.flush();
getline(std::cin, in_str);
std::cout << "Input word to be removed: ";
std::cin >> remove_str;
char_str = (char*) malloc(in_str.size()*sizeof(char));
if(!char_str)
{
std::cout << "Error allocating space for input string." << std::endl;
return -1;
}
if(!memcpy(char_str, in_str.c_str(), in_str.size()))
{
std::cout << "Error copying string to char array." << std::endl;
return -1;
}
if(!remove_string(&char_str, remove_str))
{
std::cout << "Unable to remove the string" << std::endl;
return -1;
}
in_str(char_str);
std::cout << "After eliminating \"" << remove_str <<"\" the string is: " << in_str << std::endl;
return 0;
}
/* this is where your come in,
* make the remove_string() function
* so it will remove the string provided
* in remove_str from the char array
* provided in in_str.
* It should return 1 on success and 0 on error
*/
int remove_string(char* in_str, std::string remove_str)
{
/* alot of code here where you'd loop through the in_str */
}
Now this code was just written directly into the composer window here, but It should be formed correct. You migth want to learn a bit about pointers while you're at it, since the understanding of those might come in handy.
Feel free to ask any question, if you can't quite grasp what this is all about.
|
|
|
07-26-2005, 11:52 AM
|
#5 (permalink)
|
|
C++ Beginner
Join Date: Jul 2005
Location: Ottawa
Posts: 73
|
I'll get right on it Redhead, if I get stuck somewhere I will post what I came up with, but I think I might be able to get the whole solution. I have the idea in mind, i'm just gonna start commenting that code and writing it out and then start coding it. Also, what I wanted to know was whether or not using standard naming conventions should be used in naming variables, functions, etc. I was told that for a function you should do fFunctionName or for an int iInteger or fFloat, etc. As well, capital letters on 2nd-last words....is all that necessary?
|
|
|
07-26-2005, 11:58 AM
|
#6 (permalink)
|
|
C++ Beginner
Join Date: Jul 2005
Location: Ottawa
Posts: 73
|
Also, one last question, I don't need it answered to go by with my code, however, why don't we just use the standard namespace. I realize there are a lot of them, but wouldn't they only go on at compile time, and only the ones that we call throughout our code?
|
|
|
07-26-2005, 12:19 PM
|
#7 (permalink)
|
|
Newbie
Join Date: Jun 2002
Location: Denmark
Posts: 1,721
|
Quote:
|
I was told that for a function you should do fFunctionName or for an int iInteger or fFloat, etc. As well, capital letters on 2nd-last words....is all that necessary?
|
What standard naming convention ??
I've been programming professionaly for about 7 years now, I've never encountered a standard naming convention.
You make due with what suits you best, like the ongoing indent style dispute, noone will ever settle on something that suits everybody, forinstance you'd often see me use underscores in my naming of variables/functions etc.. since I like to have describing names, that are ease readable, some might use capital letters, I belive the later is derived from Fortran and Pascal coding style, which I myself find disturbing to read.
Others might disagree on that, but thats my preference.
Quote:
|
why don't we just use the standard namespace. I realize there are a lot of them, but wouldn't they only go on at compile time, and only the ones that we call throughout our code?
|
We could do that, but beeing the wierd fellow I am, I like to control every aspect, by specificaly telling what I intend to use in my code, if I had issued using namespace std; at the top, and you find it easier to use another namespace somewhere, and tell it by issuing using namespace another somewhere in the mittle of my code, the rest of my code would be bogus, since I was assuming the rest of it woudl be using the standard namespace, instead after your small editing it is suddently usign the another namespace.
So to be on the safe side I specificaly tell what namespace I'm in at all times, eventho it's abit more code. After compiling the executable wont consume more space than if you were to globaly claim your namespace, any smart compiler will optimize on that.
|
|
|
07-27-2005, 07:46 AM
|
#8 (permalink)
|
|
Newbie
Join Date: Jun 2002
Location: Denmark
Posts: 1,721
|
Quote:
|
Also, what I wanted to know was whether or not using standard naming conventions should be used in naming variables, functions, etc.
|
I thought in this question, you might want to start with the GNU Coding Standards as a reference to how you might wanna start off with your own coding style.
|
|
|
08-02-2005, 10:46 AM
|
#9 (permalink)
|
|
Newbie
Join Date: Jun 2002
Location: Denmark
Posts: 1,721
|
Just one more for the road, I came across this description on how to comment your code it gives some idear, but like I've said earlier, make your own style that you are comfortable with.
|
|
|
10-13-2005, 06:16 PM
|
#10 (permalink)
|
|
C++ Beginner
Join Date: Jul 2005
Location: Ottawa
Posts: 73
|
Hey redhead, sorry I haven't been working on this. The concept has been way over my head for a while and I've been working on simpler tasks and trying to grasp a better understanding of functions and strings all the while. I have a question however, with your template (difference being Im using the standard namespace as I'm not used to consistently using std:: with endl, cout,cin,etc.) and I get this error (visual studio 6)
fatal error C1083: Cannot open include file: 'stdlib': No such file or directory
I've tried your code untouched and mine, as well as stdlib.h but then I get more errors as is, and I know C++ now doesn't use extensions on including header files. Before I start setting this project out (I think I can tackle it on now) I have to get this error fixed. Any suggestions?
|
|
|
10-14-2005, 01:33 AM
|
#11 (permalink)
|
|
Newbie
Join Date: Jun 2002
Location: Denmark
Posts: 1,721
|
it should be instead of the stdlib, mybad since I usualy use standard C i forgot to point it at the C++ specific stdlib header.
|
|
|
10-26-2005, 05:54 PM
|
#12 (permalink)
|
|
C++ Beginner
Join Date: Jul 2005
Location: Ottawa
Posts: 73
|
Redhead, I can't seem to figure out what I have to do with parsing out the word. I'm pretty sure it has something to do with:
Code:
char_str = (char*) malloc(in_str.size()*sizeof(char));
Is char_str the size of the word you will be removing in the phrase, and you are reserving room in memory to hold the word, for which you check against all words in that phrase through a loop until you find it? I think it's something like that but I can't figure out, I'll post what I have. The areas I didn't understand are commented
Code:
/*
Make code that removes the first encountered substring from a C-style string.
I want you to explain every line of code. You need to prove you'understand char* handling.
Like this:
char* theString = "Life is hard, then you die.";
remove_substring(theString, "hard");
//theString = "Life is, then you die."
*/
#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;
int remove_string(char* in_str, string remove_str);
int main()
{
string in_str;
string remove_str;
char* char_str; //i'm going to have to use the pointer address and length of string to remove it from the in_str?
cout << "Input string to process: ";
cout.flush();
getline(cin, in_str); //why not just cin? doesn't cin accept spaces if it processes a string?
cout << "Input word to be removed: ";
cin >> remove_str;
char_str = (char*) malloc(in_str.size()*sizeof(char)); //not sure what this does?
if(!char_str) //error checking for a non-string inputsystem("pause");
{
cout << "Error allocating space for input string." << endl;
system("pause");
return -1;
}
if(!memcpy(char_str, in_str.c_str(), in_str.size())) //an anomoly case
{
cout << "Error copying string to char array." << endl;
system("pause");
return -1;
}
system("pause");
if(!remove_string(char_str, remove_str)) //if both arguments aren't valid (remove_str doesn't match anything in in_str)
{
cout << "Unable to remove the string" << endl;
system("pause");
return -1;
}
// in_str(char_str); //Not sure what this is all about...? I want to process the remove string here don't I?
remove_string(char_str, remove_str);
cout << "After eliminating \"" << remove_str <<"\" the string is: " << in_str << endl;
system("pause");
return 0;
}
/* this is where your come in,
* make the remove_string() function
* so it will remove the string provided
* in remove_str from the char array
* provided in in_str.
* It should return 1 on success and 0 on error
*/
int remove_string(char* in_str, string remove_str)
{
cout << sizeof(in_str);
return 0;
}
|
|
|
10-27-2005, 03:30 AM
|
#13 (permalink)
|
|
Newbie
Join Date: Jun 2002
Location: Denmark
Posts: 1,721
|
Code:
char* char_str; //i'm going to have to use the pointer address and length of string to remove it from the in_str?
this is just declaring that char_str is a char* normaly what would be holding a string.
Code:
getline(cin, in_str); //why not just cin? doesn't cin accept spaces if it processes a string?
This is to ensure you get the entire line as written from the user, when you use cin >> std::string you can't be sure to get everything (space/tabs/etc) and you might end up with some garbage left in your instream.
Code:
char_str = (char*) malloc(in_str.size()*sizeof(char)); //not sure what this does?
Reserve the needed size of memory in order to have your char_str hold the in_str without creating buffer overrun or any illformed effect.
Could have been achieved with
Code:
char_str = new char(in_str.size());
aswell.
Code:
if(!char_str) //error checking for a non-string inputsystem("pause");
If the memory allocation fails, the char_str will be NULL, thus we can break off the operation at this point, since we're never gonna have expected behavior when you can't control your memory heap/stack.
Code:
if(!memcpy(char_str, in_str.c_str(), in_str.size())) //an anomoly case
Copy the user inputted string into our char_str which we at this point know will have memory enough to hold it, this could be achieved with
Code:
strcpy(char_str, in_str.substr(0));
aswell.
Code:
if(!remove_string(char_str, remove_str)) //if both arguments aren't valid (remove_str doesn't match anything in in_str)
True, so theres no need to go further, the intended word that should be removed, can't bee, if it's not pressented in the string.
Code:
// in_str(char_str); //Not sure what this is all about...? I want to process the remove string here don't I?
Reassign the char_str to in_str using the string( const char* str ); in order to let in_str hold the string with the intended word removed. So NO you don't want to process the remove string, since you've already done that.
|
|
|
10-27-2005, 03:05 PM
|
#14 (permalink)
|
|
C++ Beginner
Join Date: Jul 2005
Location: Ottawa
Posts: 73
|
How do I use the c_str() to convert my strings to an array? I can't seem to figure that out, so right now i'm going to try to index the string as is.
So far I'm trying something like this in the remove_str function, but I can't seem to quite grasp it. Am I headed in the right direction with this? If not, give me a suggestion and I'll try it out from there. Thanks
Code:
int remove_string(char* in_str, string remove_str)
{
for (int i = 0; i != (in_str.length()); i++)
{
if (strcmp(in_str[i],remove_str[i]);
in_str.erase(i,sizeof(remove_str));
}
cout << "new in_str is: " << in_str << endl;
return 0;
}
|
|
|
10-27-2005, 04:12 PM
|
#15 (permalink)
|
|
Newbie
Join Date: Jun 2002
Location: Denmark
Posts: 1,721
|
Perhaps something like:
Code:
int remove_string(char* in_str, string remove_str)
{
std::string res_str(in_str);
int i;
if(!strstr(in_str, remove_str.c_str()))
return 0;
while(string::npos != (i=res_str.find(remove_str)))
res_str.replace(i, remove_str.length(), remove_str);
return 1;
}
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -8. The time now is 03:34 AM.
|
Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting
|
 |
|