The # of Character jumps doesn't affect the result

Sep 28, 2020 at 8:42am
Write your question here.

#include <iostream>
#include <iomanip>
#include <string>
using namespace std;

int main()
{
string word;
int num;

cout<< "\nEnter a 6 letter string: "; //display word
cin>> word; //enter 6 letter word
cout << "\nEnter # of character jumps: "; //display number
cin >> num; //enter number of jumps
if (word == "0" && word == "0")
if (word == "5" && word == "5")
cout << "Result: " << word << endl;


}


Hello there I am a beginner about C++ and I'm currently working on my assignment. I wonder why The # of Character jumps doesn't affect the result. A response will be a great help to me. TYSM
Sep 28, 2020 at 8:55am
So what does 'Character jumps' mean?

Since you are not doing anything with num it cannot have any effect.

The if's don't make too much sense (comparing a 6 letter word with a single letter (twice in one if)). What are you trying to achieve there?
Sep 28, 2020 at 9:12am
Character jumps means that a user will input a number and that will lead to a character jumping for example:

Enter a 6 letter string: abcdef
Enter # of character jumps: 2 //That should make the string of abcdef into cbcdeh because the characters skipped 2 characters each.

Idk but my teacher says to only use if's and it doesn't really make sense.

I have corrected the code but my teacher wont allow me to use these commands because we're not on that topic yet. Here is the correct code.

#include <iostream>
#include <iomanip>
#include <string>
using namespace std;

int main()
{
string word;
int num;

cout<< "\nEnter a 6 letter string: ";
cin>> word;
cout << "\nEnter # of character jumps: ";
cin >> num;
cout << endl;

word.at(0) = word.at(0) + num;
word.at(5) = word.at(5) + num;

cout << "Result: " << word << endl << endl;


}



Sep 28, 2020 at 9:51am
my teacher says to only use if's and it doesn't really make sense.


What, exactly, does your assignment say. To change the 1st char and the 6th char (index 0 and 5) doesn't use if ??? Are you expected to cycle from say 'z' back to 'a'? What about lowercase & uppercase input in that case? What precisely is the requirement? If you have say 'y' and jump by 2 you get '{'. Is this what is wanted, or is 'a' expected? If 'Y' is entered is '[' expected or 'A'? What if a non-letter is entered? '1' jump 3 is '4' - or is this invalid input?

Using .at() if the length of the entered string is less than 6 chars will cause a run-time exception. It might be better if the number of entered letters is at least 6

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
#include <iostream>
#include <string>
using namespace std;

int main()
{
    string word;
    int num;

    cout<< "\nEnter a 6 letter string: ";
    cin>> word;

    if (word.size() < 6) {
        cout << "Must be at least 6 letters\n";
        return 1;
    }

    cout << "\nEnter # of character jumps: ";
    cin >> num;
    cout << endl;

    word[0] += num;
    word[5] += num;

    cout << "Result: " << word << endl << endl;
}

Last edited on Sep 28, 2020 at 10:08am
Sep 28, 2020 at 9:53am
You don't need to change the word. You may output it directly letter by letter:

cout << "Result: " << word[0] + num << word[1] << ... << word[5] + num << endl << endl;

It looks awkward but it would work (a loop would be better in that case).

However: You need if's. Consider what happens when you add 2 to 'z'?
Last edited on Sep 28, 2020 at 10:13am
Topic archived. No new replies allowed.