Chapter 3C++ Tutorial~1 min read
Input & Output
cin आणि cout — Input/Output
C++ मध्ये output साठी cout (console out) आणि input साठी cin (console in) वापरतात. हे iostream header मध्ये आहेत. << insertion operator (output), >> extraction operator (input).
cout — output
cpp
#include <iostream>
#include <iomanip> // formatting साठी
using namespace std;
int main() {
// Basic output
cout << "नमस्कार!" << endl; // endl = newline + flush
cout << "Hello" << "\n"; // \n = newline only
// Multiple values एकाच line मध्ये
string name = "Rahul";
int age = 20;
double marks = 87.5;
cout << "Name: " << name << ", Age: " << age << endl;
// Formatted output — setw, setprecision, fixed
cout << fixed << setprecision(2);
cout << "Marks: " << marks << endl; // 87.50
cout << setw(10) << "Name" << setw(8) << "Marks" << endl;
cout << setw(10) << "Rahul" << setw(8) << 87.50 << endl;
cout << setw(10) << "Priya" << setw(8) << 92.00 << endl;
return 0;
}cin — Input
cin — user कडून input
cpp
#include <iostream>
#include <string>
using namespace std;
int main() {
string name;
int age;
double marks;
cout << "नाव टाका: ";
cin >> name; // एकच word (space पर्यंत)
cout << "वय टाका: ";
cin >> age;
cout << "Marks टाका: ";
cin >> marks;
cout << "\nनमस्कार " << name << "!" << endl;
cout << "वय: " << age << " वर्षे" << endl;
cout << "Marks: " << marks << endl;
return 0;
}getline — Spaces सहित String
getline — full line input
cpp
#include <iostream>
#include <string>
using namespace std;
int main() {
string fullName;
int age;
cout << "पूर्ण नाव टाका (spaces सहित): ";
getline(cin, fullName); // पूर्ण line — spaces सोबत
cout << "वय: ";
cin >> age;
cin.ignore(); // newline buffer मधून clear करतो
cout << "Address टाका: ";
string address;
getline(cin, address);
cout << "\n--- Info ---" << endl;
cout << "नाव: " << fullName << endl;
cout << "वय: " << age << endl;
cout << "पत्ता: " << address << endl;
return 0;
}📌
cin >> ने input घेतल्यानंतर getline() call केल्यास empty string मिळते! cin >> age; नंतर cin.ignore(); call करा — newline buffer clear होतो, मग getline() बरोबर काम करते.
✅ Key Points — लक्षात ठेवा
- ▸cout << value << endl — output, endl = newline
- ▸cin >> var — एकच word/number input
- ▸getline(cin, str) — spaces सहित full line
- ▸cin.ignore() — cin >> नंतर getline() वापरायचे असेल तर
- ▸#include <iomanip> — setw(), setprecision(), fixed
0/11 chapters पूर्ण