0
hi please where the problem in my code in c++
#include <iostream> using namespace std; class Rented { public: char name[20]; float NationalID; int age; char gender; double LicenseNumber; void renteddata (char n[20], float NI, int A, char G, double LN ) { name[20] = n[20]; NationalID = NI; age = A; gender = G; LicenseNumber = LN; cout << "rented name:" << name << "\n" << "rented NationalID :" << NationalID << "\n" << "rented age" << age << "\n" << "rented gender :" << gender << "\n" << "LicenseNumber " << LicenseNumber << "\n"; } }; int main() { Rented data; data.renteddata('obaida', 12345, 25, 'm', 1111); }
3 Answers
+ 2
You have single quotes around the argument 'obaida'. Single quotes denote a single character. Use double quotes for strings.
If you are trying to copy a char array by using
name[20] = n[20];
beware that this is copying only one character, and it is indexing outside the bounds of both arrays. You should use a for loop, or a while loop, or the strcpy function, or the memcpy function.
Understand, too, that the string "obaida" is less than 20 characters long. Be sure not to copy beyond its length.
+ 1
Thx š
0
use string header file:-
#include <iostream>
#include <string>
using namespace std;
class Rented
{
public:
string name;
float NationalID;
int age;
char gender;
double LicenseNumber;
void renteddata (string n, float NI, int A, char G, double LN )
{
name = n;
............
blah blah blah
..or include <cstring) and do strcpy(name, n);....but you will get a compiler warning as your prog is C++ not C.