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.

New contributor

William Hughes is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

0

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;
}

2

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật