This is the assignment. I am having trouble understanding pointers and cannot figure this one out! My C++ course is online and the professor does not help at all lol any help or advice will be awesome!
Using pointers, have 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 right by the integer amount:
int shift;
int length;
char* str;
cout << "Input string: " << endl;
cin >> str;
cout << "How many cells would you like to shift: " << endl;
cin >> shift;
length = strlen(str);
realloc(str, length + shift); //Must expand array to fit in extra 3 spaces
for(int i = length; i >= 0; i--)
{
str[i + shift] = str[i];
str[i] = ' ';
}
cout << str;
There we go.
length is used by "for" to begin from the last cell of the character array to the start. So "01234" would be at position 4 holding '5', going till 0. I can shifting the characters shift cells in front of the current cell. Resulting in all the characters shunting shift amount of times.
haha dude I'm trying to understand but I have really bad C++ background (online courses suck) and still don't get it...This is what I have so far... if you can explain again how to make the actual word shift using that loop it would be awesome
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#include <string>
#include <iostream>
#include <string>
usingnamespace std;
int main ()
{
char * inputSTR;
char * outputSTR;
string userword;
int userint;
cout<< "please enter a 12 character word: "<<endl;
getline(cin,userword);
userword = inputSTR;
cout<<"Thanks! now enter an integer!: ";
cin>>userint;
I can not multi space in quotes, I will use 0/zero for empty value. (I'm using interchangeably for space and NULL for simplicity)
-I have typed in the string: "abcd", length of 4
-Then I have said to shift these characters 3 spaces.
-Knowing these two things, I need an array to hold 4 characters + 3 extra spaces.
-Now the character array is: "abcd000", note 3 empty spaces
-I loop from 'd' to 'a', counter will be i, holding the current position in the array.
-Assign the empty character at the end with d, then make that value empty I get: "abc000d"
-Assign the empty character at the end with c, then make that value empty I get: "ab000cd"
-Assign the empty character at the end with b, then make that value empty I get: "000bcd"
-Assign the empty character at the end with a, then make that value empty I get: "000acd"
Alright! I understand what you are saying now I want to understand it in code... This loop part is what I dont get, I understand it makes the shift process from d to a...
1 2 3 4
for(int i = length; i >= 0; i--)
{
str[i + shift] = str[i];
str[i] = ' ';
but how do I make this shift all of the letters and get the final answer of 000acd? I understand I am missing many parts to finish this code, do I need more loops? if so how many? Also the string to be shifted is supposed to have a fixed length of 12 "Using pointers, have the user input a 12 character word..." shouldn't that affect the way to go about this?
Well the <string> part I got... but that doesnt seem to be the issue lol
The code is still not working
error LNK2005: _main already defined
fatal error LNK1169: one or more multiply defined symbols found
Text with spaces in it -- like "hello world" -- doesn't work as you're using cin to get the text, which extracts up to the first space by default.
You could switch the code to use cin.getline(), but I also think that the code isn't providing space for a terminating null. So if you enter a string of 12 chars you're going to hit problems.
I would prob. use getline(cin, str), where str is a string variable, and then check the length.
Yea thanks andy! but it still cannot hold more than one word like "hello world" I think it is not a problem since it clearly asks the user to plug a 12 character word... but anyways can this be made to handle such a phrase?
Thanks! you did give me the information I didnt see it when I replied
Anyways... what do you advice me to do for the code to work (cuz I just realized it is crashing every time and when it does not it does not shift the word)... should I start using strings and "check length"using an array?