can't you explain me what wrong with my string??
it had problem with the second object!
i can't input member u of the second object;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
|
//check.h;
#ifndef CHECK_H_
#define CHECK_H_
class check{
public:
void print() const;
void set();
check();
check(char *, int);
private:
char *u;
int v;
};
//check.cpp
#include <iostream>
#include <cstring>
#include <stdio.h>
#include "check.h"
using namespace std;
void check::set(){
cout<<"Enter u:";
fflush(stdin);
cin.getline(u,10);
cout<<"Enter v:";
cin>>v;
}
check::check(){
u= new char [10];
strcpy(u,"*");
v=1;
}
check::check(char *a,int b){
u = new char [30];
u=a;
v=b;
}
void check::print() const{
cout<<"u:"<<u;
cout<<"v:"<<v;
}
#endif
//main.cpp
#include <iostream>
#include "check.h"
using namespace std;
int main() {
check object1,object2;
object1.set();
object2.set();//<== i can't set the char sequence for object2.u;
object1.print();
object2.print();
return 0;
}
|
Last edited on
Here, all clear.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
|
#include <iostream>
#include <cstring>
#include <stdio.h>
#include <iostream>
using namespace std;
class check
{
public:
void print() const
{
cout<<"u:"<<u<<endl;
cout<<"v:"<<v<<endl;
}
void set()
{
cout<<"Enter u:";
fflush(stdin);
cin.getline(u,10);
cout<<"Enter v:";
cin>>v;
}
check()
{
u= new char [10];
strcpy(u,"*");
v=1;
}
check(char *a,int b)
{
u = new char [30];
u=a;
v=b;
}
private:
char *u;
int v;
};
int main() {
check object1,object2;
object1.set();
object2.set();
object1.print();
object2.print();
return 0;
}
|
Same thing you typed. I can use the program ok.
my problem like this:
Enter u:char
Enter v:1
Enter u:Enter v:1 <=== here , i can't enter the second string;
Topic archived. No new replies allowed.