0

I want to write a program that calculates a final grade of a class based on weighted averages, and I'm in the stage of prompting the user for the names of each category (eg. 'Homework', 'Quiz', etc). I have it set up to ask the user how many categories they have, and then ask them each one individually, and then save each category name as a string into an array element. I know its probably easier to use vector class, but I'd like to do it this way if at all possible.

#include <cmath>
#include <iostream>
#include <iomanip>
#include <stdlib.h>
#include <string>
using namespace std;

int main()
{
  cout << "How many grade categories are there for this class? ";
  cin >> categories;


  int * categorynames = new int[categories];

  for (int i(0); i < categories; i++)
  {
    string text;
    cout << "Name of category: ";
    getline(cin, text);
    categorynames[i] = text;
  }

When I compile, I'm getting an error of "cannot convert std::string to int in assignment."

Can anyone help, please?

2 Answers 2

2

first of all you should change the type of categorynames into string * , also I noticed that getline is giving a whitespace as a first value (when i=0) and then will get the correct input for the rest, so change it to cin>>categorynames[i] , like this :

        string * categorynames = new string[categories];

        for (int i = 0; i < categories; i++)
        {
            //string text;
            cout << "Name of category: \n";
            cin>>categorynames[i];
            //getline(cin, text);
            //categorynames[i] = text;
        }  
Sign up to request clarification or add additional context in comments.

Comments

0

Shouldn't int * categorynames = new int[categories]; be std::string *categorynames = new std::string[categories];? and I think you can remove text and use getline(cin, categorynames[i]);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.