Sign in to follow this  
kimtuyenbui

C++ Convert Char[] Help Pleaseeeee!

Recommended Posts

hi guys! i am pretty new in C++, I am writng a simple program which makes use inheritance! everything worked just fine until i tried to convert from char to char[], I very appriciate anyone could spend a few minute and tell me what went wrong in my code! thanks before hand! here is the error i have got c:\Documents and Settings\Kim Tuyen Bui\Desktop\CW_Visual\Main\ClassRoom.cpp(12): error C2440: '=' : cannot convert from 'char []' to 'char [10]' c:\Documents and Settings\Kim Tuyen Bui\Desktop\CW_Visual\Main\Room.cpp(10): error C2440: '=' : cannot convert from 'char []' to 'char [20]' and here is some code from my program #include <iostream> #include <string> #include "stdafx.h" #include "Room.h" #include "ClassRoom.h" using namespace std; int main() { ClassRoom A1(2, 3,"M", 200, "White"); char command; cout << "\n******Please Carefully Choose One Of The Following Option*******\n\n" <<"Press C.......(class room)" <<"\n......S.......(staff room)" <<"\n......L.......(lab room)" <<"\n......M.......(meeting room)" <<"\n......D.......(meeting room & class room)" <<"\n......Q....(quit)" <<"\nEnter Your Command Please -->"; cin >>command; while (command !='q') {switch(command) { case 'c': A1.Display(),A1.ClassRoomExtra() ; break; case 's': cout<<"hi2"; break; case 'l': cout<<"hi3"; break; case 'm': cout<<"hi4"; break; case 'd': cout<<"hi5"; break; } cout << "\n******Please Carefully Choose One Of The Following Option*******\n\n" <<"Press C.......(class room)" <<"\n......S.......(staff room)" <<"\n......L.......(lab room)" <<"\n......M.......(meeting room)" <<"\n......D.......(meeting room & class room)" <<"\n......F....(To find a Room)" <<"\n......Q....(quit)" <<"\nEnter Your Command Please -->"; cin >>command; } return 0; } #pragma once #include <string> #include "Room.h" class ClassRoom: public Room { private: int capacity; char BoardType[10]; public: ClassRoom(int,int,char[20],int,char[10]); void ClassRoomExtra(); }; #include <iostream> #include <string> #include "stdafx.h" #include "ClassRoom.h" using namespace std; ClassRoom::ClassRoom(int numb = 0, int fl = 0, char build[20] =" ", int ca =0, char type[10] = "") :Room(numb, fl, build ) { capacity = ca; BoardType = type; } void ClassRoom::ClassRoomExtra() { cout <<" Capacity is "<<" "<<capacity<<" "<<"Board type is"<<" "<< BoardType<<endl; } #include <iostream> #include "stdafx.h" #include "Room.h" using namespace std; Room::Room(int numb, int fl, char build[20]) { number = numb; floor = fl; building = build; } void Room::Display() { cout << "Room number is " << number<<" At Floor "<<floor<<" In Building "<< building<< endl; } #pragma once class Room { protected: int floor; int number; char building[20]; public: Room (int, int, char[20]); void Display(); };

Share this post


Link to post
Share on other sites
ClassRoom::ClassRoom(int numb = 0, int fl = 0, char build[20] =" ", int ca =0, char type[10] = "")
:Room(numb, fl, build )
{
capacity = ca;
BoardType = type;
}



you only create default parameters in the function declaration, not the definition.

Share this post


Link to post
Share on other sites
Arrays in C++ are not mutable (although their contents are). You cannot assign an array to another.

The simplest approach is to use std::string to represent, unless you have sufficient experience, knowledge and incentive to use C-style null-terminated caracter arrays (the ones you are using now).


#include<string>

class ClassRoom: public Room
{
int capacity;
std::string BoardType;
public:
ClassRoom(int,int,const std::string&,int,const std::string);
void ClassRoomExtra();
};

// ---

#include <iostream>
#include <string>
#include "stdafx.h"
#include "ClassRoom.h"

using namespace std;

ClassRoom::ClassRoom(
int numb = 0,
int fl = 0,
const std::string& build = " ",
int ca =0,
const std::string& type = "")
:
Room(numb, fl, build),
capacity(ca),
BoardType(type)
{
}



If you still wish to handle strings, then std::strncpy, found in #include <cstring>, can help you copy the contents of the argument to the member.

Share this post


Link to post
Share on other sites
Quote:
Original post by kimtuyenbui
sorry! but can you say a bit more specific please cause im quite new to C++


When you create a default parameter move them into the header file. You are declaring the default parameters in the wrong place.

ex:


File.h

class temp
{
public:
temp(int nVal = 0, char szName = "");
}

File.cpp

temp::temp(int nVal, char szName)
{
}



Note: I am still a student, so if this information is incorrect please correct me. I don't mean to pass false information. I didn't get a chance to look at other possible mistakes, just saw this and wanted to comment.

Good luck, let me know how it works. Feel free to PM me if you still have issues.

[Edited by - JeremyCAFE on March 21, 2007 8:01:57 AM]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this