c++string

Problems with adding the result of at() to a C++ string


I am working on an assignment for my computer science course. The goal is to make a program that simplifies a very generic instruction set for tuning a harp to more readable terms.

Example: ATB+3 would become: ATB tighten 3. Or: ATB-6 would become: ATB loosen 6.

My plan was to use the at() function to add the letters to a string using it's result. Here is my code:

#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <iostream>

using namespace std;

string instructions;
string firstLetters = "";
string tightenOrLoosen;

int plusSign;
int minusSign;
int indexCount;

char repeatDecision = 'y';
char number = '0';


int main()
{
    while (repeatDecision == 'y' || repeatDecision == 'Y') {
        cout << "Hello! Please enter the harp tuning instructions here: ";
        cin >> instructions;
        plusSign = instructions.find("+");
        minusSign = instructions.find("-");
        
        if (plusSign == -1 && minusSign == -1) {
            cout << "Make sure there is a plus or minus symbol in your instructions." << endl << endl;
        } else if (plusSign != -1){
            tightenOrLoosen = "tighten";
            for (int i = 0; i > plusSign; i++) {
            firstLetters = firstLetters + instructions.at(i);
            cout << firstLetters;
            }
            number = instructions.at(indexCount);
        } else {
            tightenOrLoosen = "loosen";
            for (int i = 0; i >= minusSign; i++) {
            firstLetters = firstLetters + instructions.at(i);
            indexCount = i;
            cout << firstLetters;
            }
            number = instructions.at(indexCount);
        }
        if(plusSign != -1 || minusSign != -1) {
          cout << firstLetters << " " << tightenOrLoosen << " " << number << endl << endl; 
        }
    }
    return 0;
}

The problem is that my for loop doesn't seem to be adding anything to the "firstLetters" string. I checked couting the result while it was looping, which confirmed that the letters weren't being added to the string. I'm sure it's a stupidly simple error and I am definitely a beginner programmer, but any tips would be appreciated. Thank you.


Solution

  • Your assignment appears to involve reading input from a standard stream and parsing it. This requires breaking the input into tokens and determining whether it matches the expected format. In your case, the input consists of three distinct parts (tokens):

    Location: The first token represents the location of the string (e.g., "ATB," which I interpret as "at chord B"). Based on the context and absence of additional information, I will assume this part consists only of letters.

    Direction: The second token specifies the direction in which the tuning pin must be rotated.

    Amount: The third token indicates the degree by which the tuning pin should be rotated.

    The tokens are expected to appear in the following order: location, direction, amount.

    The challenge is to read and interpret each part correctly. Here's one possible approach:

    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main()
    {
      // read the note and direction (reads until a non-alpha character is extracted; 
      // the last read character will be stored in 'c')
      string location;
      char c; // after the loop ends, 'c' will contain the direction (if the input is correct)
      while (c = '*', cin.get(c) ) // initialize 'c' with an invalid character, then read the next character. if the read operation fails, the loop terminates.
      {
        if (isalpha(c)) // if the read charcter is a letter, add it to the location string; otherwise, break the loop
          location += c;
        else
          break; 
      }
    
      // todo: validate 'location', if requested
    
      // convert the direction
      string direction;
      switch (c)
      {
      case '-':
        direction = "loosen";
        break;
    
      case '+':
        direction = "tighten";
        break;
    
      default:
        cout << "+ or - expected" << endl;
        return -1;
      }
    
      // read the amount
      int amount;
      if (!(cin >> amount))
      {
        cout << "number expected" << endl;
        return -2;
      }
    
      // convert
      cout << location << ' ' << direction << ' ' << amount << endl;
    }