-1

Hi this is my first subject (question) at stack overflow i've tried an project about c++ classes at Code::Blocks but something went wrong

#include <iostream>
using namespace std;

class char1
{
    public:
    string charName;
    float charLength;

    void printName()
    {
        cout<<"char name is"<<charName;
    }
};

int main()
{
    int charNAME;
    float charLENGTH;

    cout<<"write your char's name"<<endl;
    cin>>charNAME;

    cout<<"write your char's length"<<endl;
    cin>>charLENGTH;

    char1 name;
    char1 length;

    name.charName=charNAME;
    length.charLength=charLENGTH;

    return 0;
}

when i run program it asks me char's name i write something, after it asks char's length but program end there i cant do anything here is picture for help

  • You want to create one `char1`, for example `char1 my_char;`. Then you want to do `my_char.charName = charName; mychar.charLength = charLength`. At this point the program is done and exists. – Robin Dillen Jan 09 '22 at 14:43
  • 2
    Is it the fact you’ve defined charNAME as int & not string? – FreudianSlip Jan 09 '22 at 14:45
  • Please do not use "using namespace std;". Please use `unsigned int` if you need some value which is from 0...n instead of float. Please use constructors to get objects initialized instead of accessing the internals directly. Please do not post pictures if we simply can have text. "but something went wrong" is also not a good error description. Do you have a beginner book for programming or c++? A good time to read the first pages, especially *why* we want to have classes and not global variables. – Klaus Jan 09 '22 at 14:49
  • @RobinDillen i write charLENGTH not charLength is program thinks two of them same? – Umut Kaya Bal Jan 09 '22 at 14:52
  • @Klaus i said i'm new i'sorry for that and also i have a beginner book about c++ – Umut Kaya Bal Jan 09 '22 at 14:57

1 Answers1

0

Some fixes for your code to show you how to use your class in practice (with some c++ coding tips).

#include <string>
#include <iostream>

// using namespace std; <== don't do this 
// https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice

//class char1
class Character // <== give classes clear names, makes code more readable
{
public:
    std::string name;
    float length;

    // I understand you want to do this to test stuff.
    // but from a design point of view printing is not something
    // a character can do to himself 
    /*
    void printName()
    {
        cout << "char name is" << charName;
    }
    */
};

// bit offtopic but : 
// if you want to add support for printing your character
// its more common to overload operator<< for streams like this
std::ostream& operator<<(std::ostream& os, const Character& character)
{
    os << "Your character's name = " << character.name << ", and his/her length = " << character.length << "\n";
    return os;
}


int main()
{
    // int charNAME; in your class this is a string, be consistent.
    // float charLENGTH;

    // make an instance of the Character class
    Character character;

    std::cout << "Enter your char's name : "; // << endl; avoid using endl use "\n" if you want a newline
    std::cin >> character.name;

    std::cout << "Enter your char's length : "; // << endl;
    std::cin >> character.length;

    std::cout << "\n";
    std::cout << character; // this will now call your overloaded << operator 
    
    return 0;
}
Pepijn Kramer
  • 9,356
  • 2
  • 8
  • 19