1

Ok so last time I asked for help on this program because I could not convert characters to DEC and add to it. I finally got it to work thanks to some advice that was given, it's almost complete.

 #include <iostream>
 using namespace std;

int main()
{
char word[128];
int x = 0;
int v;
int shift;
int sv;

cin >> shift;
cin >> word;



while (word[x] !='\0')    // While the string isn't at the end... 
{

    v = int(word[x]);


    sv = v + shift;


    x++;

   cout<< static_cast<char>(sv);

}



return 0;
}

However i have no idea how to get it to accept white spaces using

isspace

Can yall help me?

1 Answers1

0

getline in string might be your friend in this case. here is your code but fixed up to use that.

#include <string>

int rotMain()
{
  //char word[128];
  string word;
  int x = 0;
  int v;
  int shift;
  int sv;

  cin >> shift;
  getline(cin, word);

  while (word[x] != '\0')    // While the string isn't at the end... 
  {
    v = int(word[x]);
    sv = v + shift;
    x++;
    cout << static_cast<char>(sv);
  }
  return 0;
}

you're doing a few other things that are a little strange here, like you're not taking the modulus of any of your characters so, for example 'Z' rotated by 1 will be '[', but this might be by design? Also suggest iterating over the string using standard iterators, but if you're just learning don't worry about any of that for now!

aaron
  • 1,746
  • 1
  • 13
  • 24