Why is this code breaking on string input? - c++14

#include <iostream>
using namespace std;
string flip();
int x;
int main()
{
for(int i=0;i<10;++i){
cout<<"Press 1 and enter"<<endl;
cin>>x;
if(isalpha(x)==true){
cout<<"int pls"<<endl;
}
else if(x==1){
flip();
cout<<"flipped"<<endl;
}
else if(x!=1){
cout<<"try again"<<endl;
}
}
system("pause>0");
return 0;
}
string flip(){
string ans;
int y=rand()%2;
if(y==0){
string ans = "Heads";
cout<<ans<<endl;
}
else{
string ans = "Tails";
cout<<ans<<endl;
}
return ans;
}
whenever I put 2 instead of 1 it works and says try again but when i write some string like "fa"
the code closes instead of writing try again
if I change x to int and then if I try to input some string it just prints press 1 and enter 10 times instead of asking for an input again

The type of variable 'x'(=The argument of isalpha()) has to be char. If the type of 'x' is integer, the function(=isalpha()) recognizes 'x' as an ASCII value. How about trying this code?
char x;
int main()
{
for(int i=0;i<10;++i){
cout<<"Press 1 and enter"<<endl;
cin>>x;
if((bool)isalpha(x)==true){
cout<<"int pls"<<endl;
}
else if(x=='1'){
flip();
cout<<"flipped"<<endl;
}
else {
cout<<"try again"<<endl;
}
}
system("pause>0");
return 0;
}

Related

Document function Transfer to calculate the row of text file

I want to transfer a function to calculate the row of a text file.
The compile can pass but the function can not be transferred. I want to know what happens.
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
int getLine( const char *filename)
{
ifstream infile(filename,ios::in);
if(!infile){
cout<<"can not open"<<filename<<'\n';
return 0;
}
int count=0;
infile.unsetf(ios::skipws);
char buff[300];
while(infile.getline(buff,300))
count++;
cout<<"the total line:"<<count<<endl;
infile.close();
return 0;
}
int getLineNoEmpty(const char* filename)
{
ifstream infile(filename,ios::in);
if(!infile){
cout<<"can not open"<<filename<<'\n';
return 0;
}
int count=0;
char buff[300];
while(infile.getline(buff,300))
{
if(sizeof(buff)==0)
continue;
else
count++;
}
cout<<"the total line without null string:"<<count<<endl;
return 0;
}
int main()
{
char filename[256];
cout<<"input filename:";
cin>>filename;
int getLine(const char &filename);
int getLineNoEmpty(const char &filename);
return 0;
}
The compile can pass but the function can not be transferred. I want to know what happens about it. It can output the result I want. And I don't know how to
realize the goal of calculating the total line without null string.
Firstly, You are just declaring 2 functions in main() without using them. Change
int main()
{
char filename[256];
cout<<"input filename:";
cin>>filename;
int getLine(const char &filename);
int getLineNoEmpty(const char &filename);
return 0;
}
to
int main()
{
char filename[256];
cout<<"input filename:";
cin>>filename;
getLine(filename);
getLineNoEmpty(filename);
return 0;
}

Infix To postfix Evaluation using stack and array

i am trying to convert infix to postfix but unable to get any output although program is free of error and logically correct. its just showing the value of m. I tried to print hello inside loop , but its unable to print that.I can't understand why its not going inside loop. Can't figure out the exact reason behind it.
#include <bits/stdc++.h>
#define m 21
using namespace std;
int isitoperattor(char c) //to find precdenec of operator
{
if(c=='+'||c=='-')
return 1;
if(c=='*' || c=='/')
return 2;
if(c=='('||c==')')
return 3;
}
void infixtopostfix(char str[m]){
char out[m];
cout<<m<<endl;
stack <char> s;
static int k;
for(int i=0;i<m;i++)
{
cout<<"hello";
if((str[i]>='a' && str[i]<='z') || (str[i]>='A' && str[i]<='Z')){
out[k]=str[i];
k++;}
else if (str[i]=='(')
s.push(str[i]);
else if (str[i]==')'){
//int j=i-1;
while(s.top()!='('){
out[k]=s.top();
s.pop();
k++;
//-;
}
s.pop();
}
else
{
if(isitoperattor(str[i]) && (isitoperattor(str[i])<isitoperattor(s.top()))){
while(isitoperattor(s.top())>=isitoperattor(str[i])){
out[k]=s.top();
s.pop();
k++;
}
}
}
s.push(str[i]);
}
}
while(!s.empty())
{
out[k]=s.top();
s.pop();
k++;
}
//string out;
for(int j=0;j<k;j++)
cout<<out[j];
}
int main()
{
char str[m]="a+b*(c^d-e)^(f+g*h)-i";
infixtopostfix(str);
return 0;
}

Why is this code working in a different way than expected? I think its working right, is it?

Here is what I am supposed to do.
Here is what I did.(on codeblocks IDE with minGW compiler)
#include <iostream>
using namespace std;
class person
{
private:
string name;
int age;
double height;
double weight;
public:
void set_private_members(string n, int a, double h, double w)
{
name = n;
age = a;
weight = h;
height = w;
}
void print_private_members()
{
cout <<"The name of person is:- "<<name<<"\n The age of person is:- " <<age<<"\n The weight of person is:- "<<weight;
cout <<"\n The height of person is:- "<<height<<endl;
}
};
void modify_person(person);
int main()
{
person p;
p.set_private_members("Ayush",19,165.7,47.2);
cout <<"We are in main function right now \n";
p.print_private_members();
modify_person(p);
cout <<"We are now back in main function \n The values of object passed to modify_person function are as \n";
p.print_private_members();
return 0;
}
void modify_person(person z)
{
cout <<"We are in modify_person function \n Modifying person details... \n";
z.set_private_members("Priyanshi",15,159.1,50.6);
cout <<"The details are now as:- \n";
z.print_private_members();
}
Here is the output to this code
however the expected output is to have priyanshi details in the class object when print_private_members function is called in main() for the last time.
Who is wrong? Me or they? I think if I call modify_person by reference then the expected should come.

Compare source files on function level

I would like to compare two source files. While the output will return the functions that were changed. And not the lines.
My question is, what is the best why to find the start of a function and the end of it. If you know the row number.
Source files are in c.
Example
File Old
#include <stdio.h>
void checkPrimeNumber();
int main()
{
checkPrimeNumber(); // no argument is passed to prime()
return 0;
}
void checkPrimeNumber()
{
int flag=0;
printf("Enter a positive integer: ");
}
file New
#include <stdio.h>
void checkPrimeNumber();
int main()
{
checkPrimeNumber(); // no argument is passed to prime()
return 0;
}
void checkPrimeNumber()
{
int flag=1;
printf("Enter a positive integer: ");
}
Change is in fileNew flag=1;
Result will be
void checkPrimeNumber()
{
int flag=1;
printf("Enter a positive integer: ");
}

Debug Assertion Error as soon as I take the input. Something wrong with "delete"?

I am having debug assertion error as soon as I input two elements. The program was giving me access reading or sometimes writing violation error after taking 7-8 entries but after I deleted the dynamic array, it is showing debug assertion failed after taking first two inputs and breaks down. Any idea for how to solve it? I am copying only my air class here. I have similar Fire, earth and water classes too.
The error is BLOCK_TYPE_IS_VALID (pHead->nBlockUse)
Someone else too asked this question but i can't figure out My program errors. Kindly help would be appreciated.
#ifndef AIR_H
#define AIR_H
#include <iostream>
#include <string>
#include "element.h"
using namespace std;
class Air :public Element
{
public:
string air;
string Elements [2];
int i;
string *elements;
public:
Air(string n): Element (n)
{
air = n;
i=-1;
elements = new string[2];
}
void alreadyExists (string a)
{
int lineCount = 0;
ifstream read;
read.open ("Air.txt", ios::in | ios::app);
while(!read.eof())
{
string x;
read>>x;
lineCount++;
}
lineCount--;
read.close();
read.open("Air.txt", ios::in|ios::app);
for(int i = 0; i < lineCount; i++)
{
read>>elements[i];
}
bool Found = false;
for(int i = 0; i < lineCount; i++) {
if(a == elements[i])
{
Found = true;
break;
}
}
if(!Found)
{
write2file (a);
}
}
void write2file (string air)
{
ofstream write;
write.open ("Air.txt", ios::out|ios::app);
{
write<<air<<endl;
}
}
void DisplayA ()
{
/*for(int i=0; i<2; i++)//( to read through the arrays )
{
cout<<Elements[i]<<endl;
}*/
ifstream read ("Air.txt", ios::in|ios::app);
int i=0;
while (read >> Elements[i])
{
cout<<Elements[i]<<endl;
}
}
Air operator+(Air & air)
{
Air newElement ("NULL");
if (this->air == "Air"||this->air=="air"&& air.air == "Air"||air.air=="air")
{
newElement.air = "Pressure";
cout<<"Yay!! You've made: "<<newElement.air<<endl;
alreadyExists (newElement.air);
//PushA (newElement.air);
//write2file (newElement.air);
return newElement;
}
else if ((this->air == "Air"||this->air == "air") && (air.air == "Pressure"||air.air=="pressure"))/* || ((this->air == "Pressure"||this->air=="pressure") && ( air.air == "Air"||air.air=="air")))*/
{
newElement.air = "Atmosphere";
cout<<"Wuhooo!! You've made: "<<newElement.air<<endl;
alreadyExists (newElement.air);
//PushA (newElement.air);
//write2file (newElement.air);
return newElement;
}
else return newElement;
}//pressure, atmosphere
~ Air ()
{
delete []elements;
}
};
#endif
BLOCK_TYPE_IS_VALID (pHead->nBlockUse)
Assertion means a corrupt heap at deleting/clearing.
As far as I see, you missed the virtual deconstructor of Element.
Try something like:
virtual ~Element() {}
in class Element.
And please post also the Element class.
Good luck!

Resources