-
April 2nd, 2009, 04:43 PM
#1
I Hate Pointers
I don't understand how pointers work, I think the trouble I am running into is using chars for the variables. In any case I am trying to move a few variables out of one function ReverseIt() to another function Write() in any case I am running into a million errors.
Header:
Code:
#include <iostream>
#include <string>
using namespace std;
//Prototypes pointing to multiple functions
void Obj_Screen();
//must pass Reverse from ReverseIt and to Driver
void ReverseIt(char *Reverse, char *Name);
void Write(char Reverse, char Name);
Driver:
Code:
#include <iostream>
#include <string>
#include "Prototypes.h"
using namespace std;
int main()
{
char Reverse[50], Name[50];
Obj_Screen();
//recieve here
ReverseIt(&Reverse, &Name);
Write(Reverse, Name);
}
Functions:
Code:
#include <iostream>
#include <string>
#include "Prototypes.h"
using namespace std;
void Obj_Screen()
{
cout << "This Program will completly reverse the words you input into it.";
cout << "Why don't you try typing your name in first! \n\n";
system("pause");
system("cls");
}
void ReverseIt(char *Reverse, char *Name)
{
int i;
//pointer reated here, not sure if thats correct.
getline(cin, *Name);
for (i=*Name.size(); i>0; --i)
{
//need to bring this to main()
*Reverse=*Reverse+*Name[i];
}
}
void Write(char Reverse, char Name)
{
cout << "Your name in Reverse is "<<Resverse <<"!";
}
-
April 2nd, 2009, 05:05 PM
#2
Re: I Hate Pointers
Code:
for (i=*Name.size(); i>0; --i)
A char* is not the same as a string. A char* doesn't has a size function. A char* is a pointer to a array of characters, nothing more. Use strlen to get the length of your text.
Code:
*Reverse=*Reverse+*Name[i];
'Reverse' points to the first character, and you are taking to content of that character. It should look something like this.
Code:
Reverse[j]=Reverse[j]+Name[i];
j++;
Code:
void Write(char Reverse, char Name)
The 2 vars are char, not char*... also, you aren't use 'name' at all, so why put it there at all.
Last edited by Skizmo; April 2nd, 2009 at 05:07 PM.
-
April 2nd, 2009, 05:18 PM
#3
Re: I Hate Pointers
A char* is not the same as a string. A char* doesn't has a size function. A char* is a pointer to a array of characters, nothing more. Use strlen to get the length of your text.
really, will the loop in the function still work if I change the variables to Strings instead of arrays?
'Reverse' points to the first character, and you are taking to content of that character. It should look something like this.
can I have two variables in a loop? how would that look?
Last edited by ArmlessBastard; April 2nd, 2009 at 05:26 PM.
-
April 2nd, 2009, 05:42 PM
#4
Re: I Hate Pointers
Originally Posted by ArmlessBastard
really, will the loop in the function still work if I change the variables to Strings instead of arrays?
Your program already includes the header for std::string, namely <string>. So why are you resorting to using char*? Why not just use std::string in your entire program?
Regards,
Paul McKenzie
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|