Pointers

Hi, I need some help using pointers, and having the user input a 12 character word. Store this word in a char*.
Next ask the user for an integer.
Manipulate the char* to shift the word to the left by the integer amount:

>Enter a Word:
Hello World
>Enter a Number:
4

Your Shifted Word is:
rldHello Wo

>Enter a Word:
Teddy Bear
>Enter a Number:
7

Your Shifted Word is:
y BearTedd

Any help would be greatly appreciated!
What do you have so far?
It's not really a pointer issue per say. It's more a matter of shuffling items around in an array.

I'll attempt greywolf's boxes to illustrate.

Say you have a string ABC, and you want to right shif by 1 character, you'll start with an array of chars that looks like this.

1
2
3
┌─┬─┬─┐
│A│B│C│
└─┴─┴─┘


To right shift, you copy the rightmost element to a temporary. This is the overflow.
1
2
3
┌─┐
│C│
└─┘


Then move the elements over one by one.
1
2
3
4
5
6
┌─┬─┬─┐
│A│B│B│
└─┴─┴─┘
┌─┬─┬─┐
│A│A│B│
└─┴─┴─┘


Finally, you copy in the overflowed entry.
1
2
3
┌─┬─┬─┐
│C│A│B│
└─┴─┴─┘


That's what the memory looks like, you can code it without even thinking of pointers.
Here is what I have so far...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
using namespace std;

int main ()
{
    int n;
    int number [n];
    char * word;
cout << "Please enter a 12 letter word" << endl;
cin >> word;
cout << "Please Enter an integer" << endl;
cin >> n;

word[n] = 0;
word--;

return 0;
}

char * word;Don't do that. Use a std::string for a string.

Use string word;
try this...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
//I will skip preprocessor to abbreviate
string toShift;
unsigned int shiftBy; //unsigned to prevent negative shifting
char lastToFirst;

//these functions include the cout and cin parts, i'm just abbreviating
toShift = getStringFromUser();     //gets a string from user, saves it to toShift by reference
shiftBy = getIntFromUser();         //gets an integer from user, saves it to shiftBy by reference

//extracting last character, saving it to variable
lastToFirst = toShift.last(); //I don't know if this function exists. If not, u have to make it urself.

//shifting characters right one space shiftBy times
for(int i1 = 0; i1 < shiftBy; i1++){ //controlling number of times to repeat shift action
     for(int i2 = toShift.length() - 1; i2 > 0; i2--){ //controls number of characters shifted
          toShift.replace(i2, toShift.at(i2 - 1));
     }
}

toShift.replace(1, lastToFirst); //replace first character with lastToFirst

showUserResults(toShift); //abbreviated function to show user shifted string 


tell me if this works!
NOTE: you will have to #include <string> followed by using namespace std;
Last edited on
This is how it looks, but it does not work...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>
using namespace std;

int main ()
{
    
string toShift;
int shiftBy;
char lastToFirst;


getStringFromUser(&toShift);     
getIntFromUser(&shiftBy);         


lastToFirst = toShift.last(); 


for(int i1 = 0; i1 < shiftBy; i1++){ 
     for(int i2 = toShift.length() - 1; i2 > 0; i2--){ 
          toShift.replace(i2, toShift.at(i2 - 1));
     }
}

toShift.replace(1, lastToFirst); 

showUserResults(toShift); 

return 0;
}


1
2
3
string getStringFromUser();
int getIntFromUser();
void showUserResults(string toShow);


Those functions are just prototypes, you need to design them urself
1
2
#include <string>
using namespace std;


should I just give you finished code??
Last edited on
I added the #include <string>, but it still does not work. Am i designing the function properly like that? If not, how do I do that?
You have to declare the functions. Those are not pre-existing functions. You need to add them to the program. I was just trying to make my response short enough for a quick read.

here, add this BEFORE int main()...
1
2
3
string getStringFromUser();
int getIntFromUser();
void showUserResults(string toShow);


and add this AFTER int main()...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
string getStringFromUser(){
     string fromUser;
     cout << "input a string: ";
     cin >> fromUser;
     return fromUser;
}

int getIntFromUser(){
     int fromUser;
     cout << "input an integer: ";
     cin >> fromUser;
     return fromUser;
}

void showUserResults(string toShow){
     cout << "Here are the results...\n" << toShow << endl;
}



ALSO: You may want to design another function that holds the console open before your program ends so that the user can read the results.
Last edited on
Actually...if you can give me the finished code that would be great. I have had a rough day and I cannot even do the simplest of things right now
sure, no problem, I am working on it now...will be about 5 min.
EDIT: this might take longer than expected, I am going to have to redesign the replacement process.
Last edited on
You rock! Thank you
OK!! here it is!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
//PREPROCESSOR DIRECTIVES
#include <string>
#include <iostream>
using namespace std;

//FUNCTION PROTOTYPES: TELLS COMPILER THAT THESE EXIST
string getStringFromUser(); //gets a string from user
int getIntFromUser(); //gets an integer from user
void showUserResults(string toShow); //displays results
int AskUserClose(); //holds the command prompt open

//MAIN...
int main(){
//DECLARING VARIABLES...
string toShift;
string buffer; //will allow for alternating strings
int shiftBy; //unsigned to prevent negative shifting
char lastToFirst;

//ASSIGNING VARIABLES...
//these functions include the cout and cin parts, i'm just abbreviating
toShift = getStringFromUser(); //gets a string from user, saves it to toShift by reference
shiftBy = getIntFromUser(); //gets an integer from user, saves it to shiftBy by reference

//extracting last character, saving it to variable
lastToFirst = toShift.at(toShift.length() - 1);

buffer.clear(); //clearing buffer
//shifting characters right one space shiftBy times by alternation
for(int i1 = 0; i1 < shiftBy; i1++){ //controlling number of times to repeat shift action
	//moving last character of shiftBy to first position in buffer
     buffer.push_back(toShift[toShift.length() - 1]);
	 //moving the rest of the letters into buffer in original order
	 for(unsigned int i2 = 0; i2 < toShift.length() - 1; i2++){
		 buffer.push_back(toShift[i2]);
	 }
	 toShift.assign(buffer);
	 buffer.clear();
}

showUserResults(toShift); //abbreviated function to show user shifted string

//this holds console open so user can see results
	return AskUserClose();
}

//FUNCTIONS: COMPILER READS FUNCTIONS FROM HERE
//gets a string from user
string getStringFromUser(){
     string fromUser;
     cout << "input a string: ";
     cin >> fromUser;
     return fromUser;
}

//gets an integer from user
int getIntFromUser(){
     int fromUser;
     cout << "input an integer: ";
     cin >> fromUser;
     return fromUser;
}

//shows user results
void showUserResults(string toShow){
     cout << "Here are the results...\n" << toShow << endl;
}

//holds the command prompt open for user to see results
int AskUserClose(){
	int input;
	cout << "The program has finished.\nEnter ANYTHING to close the window.\n";
	cin >> input;
	return(0);
}
It works great! Thanks so much!
no problem! =]
Topic archived. No new replies allowed.