PF LAB 12
PF LAB 12
get
Garbage value in char array?
#include <iostream>
using namespace std;
int main()
{
int age;
char name[4];
cin.ignore();
char arr2[3];
cin.getline(arr2, 3); // Reads the full line
cout<<endl<<"arr2 "<<arr2;
return 0;
}
void addStudent() {
ofstream outFile("students.txt", ios::app); // Open file in append mode
if (!outFile) {
cout << "Error opening file!" << endl;
return;
}
string name;
int rollNo, marks;
cout << "Enter Roll Number: ";
cin >> rollNo;
cout << "Enter Name: ";
cin.ignore();
getline(cin, name);
cout << "Enter Marks: ";
cin >> marks;
outFile << rollNo << " " << name << " " << marks << endl; // Write to file
outFile.close();
cout << "Student record added successfully!" << endl;
}
string line;
cout << "\nStudent Records:" << endl;
while (getline(inFile, line)) {
cout << line << endl; // Display each line in the file
}
inFile.close();
}
#include <iostream>
#include <cmath>
#include <iomanip>
// Function prototypes
bool isPrime(int n);
float formula1(float n);
float formula2(float n);
void processArray(float arr[], int size);
int main() {
// Declare an array of floating-point numbers (n > 10)
const int n = 12;
float arr[n] = {2, 4, 3, 5, 7, 6, 8, 9, 11, 13, 14, 17};
// Display the array before processing
std::cout << "Array before processing: ";
for (int i = 0; i < n; i++) {
std::cout << arr[i] << " ";
}
std::cout << std::endl;
return 0;
}
// Formula 1: (1 - n^2) / (n + 2)
float formula1(float n) {
return (1 - n * n) / (n + 2);
}