Skip to content

Commit 3d01840

Browse files
committed
[CPP] Add simple cin
1 parent f23b9bb commit 3d01840

File tree

4 files changed

+24
-3
lines changed

4 files changed

+24
-3
lines changed

src/usr/include/iostream.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,14 @@ class ostream {
1313
ostream& operator<<(const int num);
1414
};
1515

16+
class istream {
17+
public:
18+
istream& operator>>(char &c);
19+
istream& operator>>(char *str);
20+
istream& operator>>(int &num);
21+
};
22+
1623
extern ostream cout;
24+
extern istream cin;
1725

1826
} // namespace std end

src/usr/lib/iostream.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
#include <iostream.h>
22
#include <stdio.h>
3+
#include <conio.h>
34

45
namespace std {
56

67
const char endl = '\n';
78
ostream cout;
9+
istream cin;
810

911
ostream& ostream::operator<<(const char c) { putchar(c); return *this; }
1012
ostream& ostream::operator<<(const char *str) { puts(str); return *this; }
1113
ostream& ostream::operator<<(const int num) { printf("%d", num); return *this; }
1214

15+
istream& istream::operator>>(char &c) { c = getch(); putchar(c); return *this; }
16+
istream& istream::operator>>(char *str) { gets(str); return *this; }
17+
1318
} // namespace std end

src/usr/local/src/logo.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// logo program
2+
#include <iostream.h>
23
#include <stdio.h>
34
#include <conio.h>
45
#include <graphics.h>
@@ -46,7 +47,7 @@ int main(int argc,char *argv[]) {
4647
std::graphics::initgraph(&gd, &gm, NULL);
4748
int gerr = std::graphics::graphresult();
4849
if (gerr != 0) {
49-
std::printf("failed to open graphics mode, err: %d", gerr);
50+
std::cout << "failed to open graphics mode, err: " << gerr << std::endl;
5051
return 1;
5152
}
5253
logo();

src/usr/local/src/simplecpp.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
// Simple C++ program
2-
32
#include <stdio.h>
43
#include <iostream.h>
54

@@ -50,12 +49,20 @@ void printShapeDetails(geometry::Shape *shape) {
5049
std::cout << " - Name: " << shape->getName() << std::endl;
5150
std::cout << " - Area: " << (int)shape->getArea() << std::endl;
5251
}
53-
52+
#include <string.h>
5453
int main(int argc, char *argv[]) {
54+
// shapes
55+
5556
geometry::Rectangle r1("R1", 10, 20);
5657
geometry::Square s1("S1", 50);
5758

5859
printShapeDetails(&r1);
5960
printShapeDetails(&s1);
61+
62+
// name
63+
char name[1024];
64+
std::cout << "Enter your name: ";
65+
std::cin >> name;
66+
std::cout << "Your name is: " << name << std::endl;
6067
return 0;
6168
}

0 commit comments

Comments
 (0)