I cant use the s1[40] for the second time or longer in the circle and it's always full,
and cin.getline(s1,40)
is ignored at later times
char s1[40], ans = 'y';
while (ans == 'y')
{
system("cls");
cout << "\n Enter a sentence : ";
cin.getline(s1, 40);
fflush(stdin);
cout << "\n________________________________________\n\n Again?(y/n)";
cin >> ans;
fflush(stdin);
};
cin >> ans; will actually not remove eol so next getline will read an empty line and cin >> ans; will read first symbol of line. You should make ans an array as well use getline twice:
for(;;)
{
char s1[40]{};
system("cls");
cout << "\n Enter a sentence : ";
cin.getline(s1, 40);
fflush(stdin);
if(cin.fail() or cin.bad())
{
cout << "fail" << endl;
break;
}
cout << "\n________________________________________\n\n Again?(y/n)";
char ans[2]{};
cin.getline(ans, 2);
fflush(stdin);
if(cin.fail() or cin.bad())
{
cout << "fail" << endl;
break;
}
if(0 != strcmp("y", ans))
{
break;
}
}
Related
void addItem(struct InventoryItem inventory[], int& size){
cout << "\nEnter the item name: ";
cin.getline(inventory[size].itemName, 100, '\n');
while (strlen(inventory[size].itemName) == 0){
cout << "Invalid item name!" << endl;
cout << "Enter item name: ";
cin.getline(inventory[size].itemName, MAX_CHAR, '\n');
}
cout << "Enter the item price: ";
cin >> inventory[size].itemPrice;
while(!cin || inventory[size].itemPrice < 0){
cout << "Please enter a valid number: ";
cin >> inventory[size].itemPrice;
clear();
}
cout << endl;
size++;
}
My !cin keeps accepting letters as numbers ...
any ideas why?
note: I'm new to programming.
If operator>> fails to extract a value, it puts the input stream into an error state. You are not clearing that state, or ignoring the bad input that caused it to fail, so operator>> keeps failing.
Try this instead:
void addItem(struct InventoryItem inventory[], int& size){
do {
cout << "Enter the item name: ";
if (cin.getline(inventory[size].itemName, MAX_CHAR, '\n')) {
if (strlen(inventory[size].itemName) > 0) {
break;
}
}
else {
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
}
cout << "Invalid item name!" << endl;
}
while (true);
do {
cout << "Enter the item price: ";
if (cin >> inventory[size].itemPrice) {
if (inventory[size].itemPrice >= 0) {
break;
}
}
else {
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
}
cout << "Invalid item price!" << endl;
}
while (true);
cout << endl;
size++;
}
I'm trying to use a loop to verify that the input grade is an integer, but when I intentionally input a char, the program does nothing. It only starts a newline upon pressing enter.
void fillvector(vector<student>& parameter)
{
string newname;
int newgrade;
int number;
cout << "How many students are in your class: ";
cin >> number;
for (int i = 0; i < number; i++)
{
cout << endl << "Enter student name: ";
while (1)
{
cin >> newname;
cout << "Enter student grade: ";
cin >> newgrade;
if (cin.fail())
{
cout << endl << "Grade must be an integer value, try again.";
cin.clear();
cin.ignore(INT_MAX);
i--;
break;
}
else
{
student student(newname, newgrade);
parameter.push_back(student);
break;
}
}
}
cout << endl;
}
cin.ignore(INT_MAX);
This will make cin ignore any further characters you enter up to MAX_INT..
What you wanted is to ignore up-to the next newline:
cin.ignore(INT_MAX, '\n');
Besides, there is no point in making a while(1) loop that will break in all paths in the first iteration. You can remove that inner loop.
So i have a palindrome program and here are the codes:
#include <iostream>
#include <string>
#include <cctype>
using namespace std;
void palindrome();
void compareTwoInt();
bool validation(const string&);
int main()
{
int selection;
cout << "\t\t\t MENU\n";
cout << "\t\t\t ----\n";
cout << "\t\t\t1. Palindrome";
cout << "\n\t\t\t2. Compare Two Integers";
cout << "\n\t\t\t3. End program\n";
cout << "\n\t\t\tEnter your choice : ";
cin >> selection;
while (selection < 0 || selection > 4)
{
cout << "\t\t\nInvalid entry. Please enter an appropriate entry.";
cout << "\n\n \t\t\tEnter your choice: ";
cin >> selection;
}
if (selection == 1)
{
cout << "Enter a word, phrase, sentence: \n";
string input;
getline(cin, input);
string input2;
for (unsigned int i = 0; i < input.length(); i++)
{
if (isalnum(input[i]))
{
input2 += toupper(input[i]);
}
}
cout << input2 << endl;
if (validation(input2))
{
cout << "The phrase is a palindrome!" << endl;
cout << "Press <Enter> key back to menu" << endl;
}
else
{
cout << "The phrase is not a palindrome!" << endl;
cout << "Press <Enter> key back to menu" << endl;
}
fflush(stdin);
cin.get();
system("cls");
return main();
}
else if (selection == 2)
{
compareTwoInt();
fflush(stdin);
system("cls");
return main();
}
else if (selection == 3)
{
cout << "\t\t Good Bye. Press <Enter> key to End the program.\n";
}
fflush(stdin);
cin.get();
return 0;
}
void compareTwoInt()
{
int first, second;
cout << "\n\nEnter your positive integer : ";
cin >> first;
cout << "\nEnter your positive integer : ";
cin >> second;
fflush(stdin);
cin.get();
}
bool validation(const string& input)
{
return input == string(input.rbegin(), input.rend());
}
for some reason when i choose 1 for the palindrome, it doesn't let me write the words, (in another words, it doesn't let me input)
the console just says:
Enter a word, phrase, sentence:
The phrase is palindrome!
Press key back to menu
Anybody have an idea how to fix this?
Thanks in advance!
When you choose 1 for the palindrome, you hit enter. Thus your input consists of the number 1 followed by a newline. Your cin >> selection; reads the number 1 and then your getline(cin, input); reads the newline, which it interprets as an empty line. You have written no code to do anything sensible with the newline character input after the number, so nothing sensible happens.
Try typing 1foof<enter> instead. Your code will read that as a 1 followed by a line containing foof.
I have this program in C++ for Student Management System , Everything is working fine , except one place where i try to delete a student based on his roll number .
What it should do : After asking the roll number search the record and delete it
What is it doing : It deletes all the other records which do not match that roll number
Here is my code :
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <conio.h>
#include <iomanip>
using namespace std;
int main() {
FILE *fp, *ft;
char another, choice;
struct student {
char first_name[50], last_name[50];
int roll_num; //new code added
char course[100];
int section;
};
struct student e;
char xfirst_name[50], xlast_name[50];
int xroll_num ; // new code added
long int recsize;
fp=fopen("users.txt","rb+");
if (fp == NULL) {
fp = fopen("users.txt","wb+");
if (fp==NULL)
{
puts("Cannot open file");
return 0;
}
}
recsize = sizeof(e);
while(1) {
system("cls");
cout << "\t\t====== STUDENT INFORMATION SYSTEM ======";
cout <<"\n\n ";
cout << "\n\n";
cout<<" \n\t\t\t======================";
cout << "\n \t\t\t 1. Add Records";
cout << "\n \t\t\t 2. List Records";
cout << "\n \t\t\t 3. Modify Records";
cout << "\n \t\t\t 4. Delete Records";
cout << "\n \t\t\t 5. Exit Program";
cout<<" \n\t\t\t======================";
cout << "\n\n";
cout << "\t\t\t Select Your Choice ::";
fflush(stdin);
choice = _getche();
switch(choice)
{
case '1' :
fseek(fp,0,SEEK_END);
another ='Y';
while(another == 'Y' || another == 'y')
{
system("cls");
cout << "Enter the First Name : ";
cin >> e.first_name;
cout << "Enter the Last Name : ";
cin >> e.last_name;
cout << "Enter the Course : ";
cin >> e.course;
cout << "Enter the Section : ";
cin >> e.section;
cout << "Enter the roll number :";
cin >> e.roll_num;
fwrite(&e,recsize,1,fp);
cout << "\n Add Another Record (Y/N) ";
fflush(stdin);
another = getchar();
}
break;
case '2':
system("cls");
rewind(fp);
cout << "=== View the Records in the Database ===";
cout << "\n";
while (fread(&e,recsize,1,fp) == 1){
cout << "\n";
cout <<"\nName :: " <<e.first_name <<" "<<e.last_name;
//cout << "\n";
cout <<"\nRoll Number :: " << e.roll_num ;
cout <<"\nCourse :: " <<e.course ;
cout <<"\nSection :: "<<e.section;
}
cout << "\n\n";
system("pause");
break;
case '3' :
system("cls");
another = 'Y';
while (another == 'Y'|| another == 'y')
{
// cout << "\n Enter the last name of the student : ";
cout << "\n Enter the Roll number of the student : ";
cin >> xroll_num;
rewind(fp);
while (fread(&e,recsize,1,fp) == 1)
{
//if (strcmp(e.last_name,xlast_name) == 0)
if(e.roll_num == xroll_num )
{
cout << "Enter the new Firt Name : ";
cin >> e.first_name;
cout << "Enter the new Last Name : ";
cin >> e.last_name;
cout << "Enter the new Roll Number : ";
cin >> e.roll_num;
cout << "Enter the new Course : ";
cin >> e.course;
cout << "Enter the new Section : ";
cin >> e.section;
fseek(fp, - recsize, SEEK_CUR);
fwrite(&e,recsize,1,fp);
break;
}
else
cout<<"record not found";
}
cout << "\n Modify Another Record (Y/N) ";
fflush(stdin);
another = getchar();
}
break;
case '4':
system("cls");
another = 'Y';
while (another == 'Y'|| another == 'y')
{
// cout << "\n Enter the last name of the student to delete : ";
cout <<"\n Enter the roll number of the student to delete : ";
cin >> xroll_num;
ft = fopen("temp.dat", "wb");
rewind(fp);
while (fread (&e, recsize,1,fp) == 1)
// if (strcmp(e.last_name,xlast_name) != 0)
if(e.roll_num == xroll_num )
{
fwrite(&e,recsize,1,ft);
}
fclose(fp);
fclose(ft);
remove("users.txt");
rename("temp.dat","users.txt");
fp=fopen("users.txt","rb+");
cout << "\n Delete Another Record (Y/N) ";
fflush(stdin);
another = getchar();
}
break;
case '5':
fclose(fp);
cout << "\n\n";
cout << "\t\t THANK YOU FOR USING THIS SOFTWARE";
cout << "\n\n";
exit(0);
}
}
system("pause");
return 0;
}
It deletes all the other records which do not match that roll number
Well you're only writing the records that match the roll number to the temp file, and then using that file to overwrite the users.txt file
if (e.roll_num == xroll_num) {
fwrite(&e, recsize, 1, ft);
}
I suppose what you really wanted to do is
if (e.roll_num != xroll_num) {
fwrite(&e, recsize, 1, ft);
}
You should probably read a good C++ i/o tutorial, as your code is mostly C. Consider writing your student struct as simple text instead of writing it wholesale to the file.
So I have these two functions at the top of my program:
string deletespaces(string sentence){
sentence.erase(std::remove(sentence.begin(), sentence.end(), ' '), sentence.end());
return sentence;
}
and
string checkpalindrome(string sentence){
deletespaces(sentence);
if (sentence == string(sentence.rbegin(), sentence.rend())){
cout << sentence << " is a palindrome." << endl;
}
if(sentence != string(sentence.rbegin(), sentence.rend())){
cout << sentence << " isn't a palindrome." << endl;
}
}
This is my main body of code.
int main(){
int x = 1;
string originalsentence;
while (x == 1) {
int a = 1;
int input;
string sentence;
cout << "Press 1 to reverse a string, 2 to check a palindrome, and anything else to quit: ";
cin >> input;
if(input == 1) {
cout << "Enter a sentence to reverse: ";
cin.ignore();
getline(cin,originalsentence);
originalsentence = sentence;
cout << string(sentence.rbegin(), sentence.rend()) << endl;
}
if(input == 2) {
cout << "Enter a sentence to check: ";
cin.ignore();
getline(cin,originalsentence);
originalsentence = sentence;
checkpalindrome(sentence);
}
cout << "Hit 1 if you want to continue, anything else to quit: ";
cin >> x;
}
}
So here is my problem. I am able to put in my choice about whether to reverse a string or check whether the string is a palindrome. However, when I try to reverse the string, it simply gives a blank line, then asks whether I want to continue. When I ask to check a palindrome, it simply says "is a palindrome" regardless of what I input. Any ideas?