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:
#include <iostream>
usingnamespace 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;
}
//I will skip preprocessor to abbreviate
string toShift;
unsignedint 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 usingnamespace std;
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.
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.
//PREPROCESSOR DIRECTIVES
#include <string>
#include <iostream>
usingnamespace 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(unsignedint 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);
}