fatal error in vector use - string

For a school c++ lab (using microsoft visual studio, hence the system("pause")) I am making a program that will let a user input an email address and the program will spit out the username (before '#') and the site type, either the type based on the last three letters of the address (com is commercial ventures) or the last two letter country code (us is united states).
#include <iostream>
#include <string>
using namespace std;
void getemail(string &email);
void finduser(string email);
void findsitetype(string email);
int main()
{
string email;
getemail(email);
finduser(email);
findsitetype(email);
system("pause");
return 0;
}
void getemail(string &email)
{
cout << "Please enter your email address: ";
cin >> email;
cout << endl;
}
void finduser(string email)
{
int index = email.find('#');
cout << "Username: ";
for (int i = 0; i < index; i++)
cout << email[i];
cout << endl << endl;
}
void findsitetype(string email)
{
int truesize = size(email);
string lastthree;
for (int i = 0; i < 3; i++)
{
lastthree[i] = email[truesize - i];
}
cout << "Site type: ";
if (lastthree == "edu")
cout << "Educational institutions";
if (lastthree == "org")
cout << "Not-for-profit organizations";
if (lastthree == "gov")
cout << "Government entities";
if (lastthree == "mil")
cout << "Military installations";
if (lastthree == "net")
cout << "Network service providers";
if (lastthree == "com")
cout << "Commercial ventures";
if (email[truesize - 2] == '.')
cout << "Country Code " << email[truesize - 1] << email[truesize];
}
When I run the code, it spits out the username but there seems to be a fatal error when finding the site type. I think it has something to do with my incorrect string use? Any help appreciated.
Debug Assertion Failed!
Program: C:\windows\SYSTEM32\MSVCP140D.dll File: c:\program files
(x86)\microsoft visual studio 14.0\vc\include\xstring Line: 1681
Expression: string subscript out of range
For more information on how your program can cause an assertion
failure, see the Visual C++ documentation on asserts.
(Press Retry to debug the application)

There are a couple things wrong with your code, (1) to get the length of a string, use the length() function, so:
int truesize = size(email);
should be
int truesize = email.length();
I changed your if statements to else ifs because if one of the condition statements evaluates to true, we shouldn't need to check the rest of them.
(2) your for loop is grabbing the email extension in the reverse direction, change:
for (int i = 0; i < 3; i++)
{
lastthree[i] = email[truesize - i];
}
to
lastthree = email.substr(truesize-3, truesize);

Related

for loop not running in C plus plus program, without showing any error - CLOSED

It is a very simple program and I have just started learning c++ but the for loop is not working in this program. can anyone tell me why the for loop in this c++ program is not running?
#include <iostream>
#include <string>
using namespace std;
class PrimeNo
{
int a;
int c;
public:
// function to check prime number
void chk_Prime(void)
{
cout << "Enter the number: " << endl;
cin >> a;
c=a/2; // to store the condition
for (int i = 2; i < c; ++i)
{
if (a == 0 || a == 1)
{
cout << "The number is not a prime number" << endl;
break;
}
else if (a % i == 0)
{
cout << "This number is not a prime number" << endl;
break;
}
else //if upper conditions are false a number is a prime number
{
cout << "This number is a prime number" << endl;
break;
}
}
}
};
int main()
{
PrimeNo Num;
Num.chk_Prime();
return 0;
}

This declaration has no storage class or type specifier Error

Hello Everyone! I have a Project due tomorrow and I accidently chosen a hard one and now im regretting it. Im trying to make digital voting system on a small scale just to represent my idea as I'm a 2nd semester student of BSCS. I made the entire code by myself. The first code I made was working code but the output was not as I desired. I wanted to change the Vote after casting the vote. But it wasnt changing it. This is the first code Below:
#include<iostream>
#include<conio.h>
using namespace std;
void VoteLIST();
void VoteReturn();
class NADRA
{
protected:
double nic[4] = { {42401335},{1},{2},{3} };
int knum[4] = { {123},{1},{2},{3} };
int vote[4] = { {0},{0},{0},{0} };
void voteCHANGE(int a, int b)
{
vote[a] = b;
}
};
class CNIC :protected NADRA
{
private:
//nic = <=
double cnic;
int KhandaanNUM;
public:
CNIC()
{
cnic = KhandaanNUM = 0;
}
CNIC(double a, int b)
{
cnic = a;
KhandaanNUM = b;
}
void SetDATA(double a, int b)
{
cnic = a;
KhandaanNUM = b;
}
double getCNIC()
{
return cnic;
}
int getKNUM()
{
return KhandaanNUM;
}
int voteSTATUS = 0;
bool check()
{
for (int loop = 0; loop < 4; loop++)
{
if (cnic == nic[loop] && KhandaanNUM == knum[loop] && (vote[loop] == 0))
{
voteSTATUS = vote[loop];
return true;
break;
}
}
return false;
}
void setVOTE(int c)
{
//vote[voteSTATUS] = c;
voteCHANGE(voteSTATUS, c);
}
void VoteReturn()
{
cout << voteSTATUS;
}
};
class VOTEmenu
{
private:
CNIC c1;
double Cnic;
int kNum;
public:
void GetData()
{
cout << "Enter CNIC: ";
cin >> Cnic;
cout << "Enter Khandaan Number: ";
cin >> kNum;
c1.SetDATA(Cnic, kNum);
}
void checkData()
{
if (c1.check() == true)
{
int choice;
system("cls");
VoteLIST();
cout << endl;
cout << "Please choose the Respective Party Number" << endl;
cin >> choice;
c1.setVOTE(choice);
system("cls");
cout << "You have successfully voted for Party Number: " << choice <<endl;
}
else
{
system("cls");
cout << "You are not Eligible to Cast Vote!" << endl;
cout << "Reasons:\n1) CNIC or Khandaan Number You entered may be Incorrect";
cout << "\n2) You may not be 18 Above\n3) You may have already casted the vote";
}
}
void Data2()
{
if (c1.check() == true)
{
system("cls");
cout << "Your CNIC Number: " << Cnic << endl;
cout << "Khandaan Number: " << kNum << endl;
cout << "You Have Voted for party number: "; c1.VoteReturn();
}
else
{
system("cls");
cout << "You are not Eligible to Cast Vote!" << endl;
cout << "Reasons:\n1) CNIC or Khandaan Number You entered may be Incorrect";
cout << "\n2) You may not be 18 Above\n3) You may have already casted the vote";
}
}
};
void main()
{
while (true)
{
VOTEmenu v;
int choice;
system("cls");
cout << "Welcome To Vote Menu!" << endl;
cout << "Please choose any one of the following" << endl;
cout << "1) Cast Vote\n2) Check Vote";
cout << endl;
cin >> choice;
v.GetData();
if (choice == 1)
{
v.checkData();
_getch();
}
else if (choice == 2)
{
v.Data2();
_getch();
}
else
{
cout << "Wrong DATA:" << endl;
}
}
}
void VoteLIST()
{
cout << "VOTE LIST" << endl;
cout << "1: PTI\n2:PMLN\n3:PPP\n4:MQM" << endl;
}
Now I tried to remake the code and added file stream. taking text from external source instead of making array inside the visual studio and assigning values. but now Im facing an error which says This declaration has no storage class or type specifier The code is below:
#include<iostream>
#include<conio.h>
#include <fstream>
using namespace std;
void VoteLIST();
void VoteReturn();
class NADRA
{
protected:
fstream Text;
int nic[2];
int knum[2];
int vote[2];
int count = 0;
int ccnic = 0;
int knumm = 0;
int votee = 0;
Text.open("Nadra.txt")
{
while (!Text.eof())
{
Nadra >> ccnic >> knumm >> votee;
nic[count] = ccnic;
knum[count] = knumm;
vote[count] = votee;
count++;
}
}
Text.close();
void voteCHANGE(int a, int b)
{
vote[a] = b;
}
};
class CNIC :protected NADRA
{
private:
//nic = <=
double cnic;
int KhandaanNUM;
public:
CNIC()
{
cnic = KhandaanNUM = 0;
}
CNIC(double a, int b)
{
cnic = a;
KhandaanNUM = b;
}
void SetDATA(double a, int b)
{
cnic = a;
KhandaanNUM = b;
}
double getCNIC()
{
return cnic;
}
int getKNUM()
{
return KhandaanNUM;
}
int voteSTATUS = 0;
bool check()
{
for (int loop = 0; loop < 4; loop++)
{
if (cnic == nic[loop] && KhandaanNUM == knum[loop] && (vote[loop] == 0))
{
voteSTATUS = vote[loop];
return true;
break;
}
}
return false;
}
void setVOTE(int c)
{
//vote[voteSTATUS] = c;
voteCHANGE(voteSTATUS, c);
}
void VoteReturn()
{
cout << voteSTATUS;
}
};
class VOTEmenu
{
private:
CNIC c1;
double Cnic;
int kNum;
public:
void GetData()
{
cout << "Enter CNIC: ";
cin >> Cnic;
cout << "Enter Khandaan Number: ";
cin >> kNum;
c1.SetDATA(Cnic, kNum);
}
void checkData()
{
if (c1.check() == true)
{
int choice;
system("cls");
VoteLIST();
cout << endl;
cout << "Please choose the Respective Party Number" << endl;
cin >> choice;
c1.setVOTE(choice);
system("cls");
cout << "You have successfully voted for Party Number: " << choice << endl;
}
else
{
system("cls");
cout << "You are not Eligible to Cast Vote!" << endl;
cout << "Reasons:\n1) CNIC or Khandaan Number You entered may be Incorrect";
cout << "\n2) You may not be 18 Above\n3) You may have already casted the vote";
}
}
void Data2()
{
if (c1.check() == true)
{
system("cls");
cout << "Your CNIC Number: " << Cnic << endl;
cout << "Khandaan Number: " << kNum << endl;
cout << "You Have Voted for party number: "; c1.VoteReturn();
}
else
{
system("cls");
cout << "You are not Eligible to Cast Vote!" << endl;
cout << "Reasons:\n1) CNIC or Khandaan Number You entered may be Incorrect";
cout << "\n2) You may not be 18 Above\n3) You may have already casted the vote";
}
}
};
void main()
{
while (true)
{
VOTEmenu v;
int choice;
system("cls");
cout << "Welcome To Vote Menu!" << endl;
cout << "Please choose any one of the following" << endl;
cout << "1) Cast Vote\n2) Check Vote";
cout << endl;
cin >> choice;
v.GetData();
if (choice == 1)
{
v.checkData();
_getch();
}
else if (choice == 2)
{
v.Data2();
_getch();
}
else
{
cout << "Wrong DATA:" << endl;
}
}
}
void VoteLIST()
{
cout << "VOTE LIST" << endl;
cout << "1: PTI\n2:PMLN\n3:PPP\n4:MQM" << endl;
}
Can Someone please help??? Tomorrow is my last date! I just want my program to take data from text, chech the data with user entered data, IF correct, it will proceed and show for vote menu. if not, It will give error etc.
I know I'm a very pathetic learner. But You guys are professional! I will learn alot from you. Please help me!

Can't initialize float array with {0}

My Compiler gives runtime error when I initialize the float array in Veccreator function. I am here posting just a sample of what my code looks like.
#include<iostream>
using namespace std;
#define SIZE 1000
class Vector
{
private:
float vecarray[SIZE];
public:
void VecCreator(int dimension)
{
vecarray[SIZE]= { 0 };
cout << "Enter " << dimension << " digits" << endl;
for (int i = 0; i < dimension; i++)
{
cin >> vecarray[i];
}
}
};
int main(void) {
Vector obh;
obh.VecCreator(2);
}
But it works fine with this:`
#include<iostream>
using namespace std;
#define SIZE 1000
class Vector
{
private:
float vecarray[SIZE]= {0};
public:
void VecCreator(int dimension)
{
cout << "Enter " << dimension << " digits" << endl;
for (int i = 0; i < dimension; i++)
{
cin >> vecarray[i];
}
}
};
int main(void) {
Vector obh;
obh.VecCreator(2);
}
Please tell me why the first code is giving error.
Look at the second answer here:
https://social.msdn.microsoft.com/Forums/vstudio/en-US/14e7318e-6fff-4d68-a823-9cbe7b7bc20a/debugging-runtime-check-failure-2-stack-around-the-variable-loggerthread-was-corrupted?forum=vcgeneral
why not do it like here below? i mean if you want to put values in there, why initially put 0 in there?
private:
float vecarray[SIZE];
public:
void VecCreator(int dimension)
{
cout << "Enter " << dimension << " digits" << endl;
for (int i = 0; i < dimension; i++)
{
cin >> vecarray[i];
}
}

Why the variable 'len_' never been change in the function ca_time

void ca_time(int *arr,int &len_) //the variable len_'s value always is 0
{
cout << "You only got five seconds to type the number 1~5,,ready go,,\n";
_sleep(5000);
cout << "Sorry time out!!\n";
cout << "Ok, here is your greads:\n";
for(int i = 0; i < len_; i ++)
{
cout << arr[i] << " ";
}
cout << endl;
cout <<"-->" << len_ << endl;
return;
}
void fun_type(int *arr,int &len_)
{
memset(arr,'\0',sizeof(arr));
for(; len_ < 5; len_ ++)
{
cin >> arr[len_];
}
}
int main()
{
int arr[100];
int len = 0;
thread time(ca_time,arr,len);
time.detach();
fun_type(arr,len);
system("pause");
return 0;
}
But it work when changed the quote to the address(point variable).Why?
Somebody say that's a IED's bug?But I don't think so .So What the hell?

Can't find the error

This program is a nightmare, it wont even give me errors when ran, visual studios tells me nothing and i need some help
#include <iostream>
using namespace std;
class Textbook
{
private:
char *aPtr;
char *tPtr;
int yearPub;
int numPages;
char bookType;
public:
Textbook(char *, char *, int, int, char);
void display();
void operator=(Textbook&);
};
Textbook::Textbook(char*string = NULL, char*string2 = NULL, int ypub = 0, int npages = 0, char btype = 'X')
{
aPtr = new char[strlen(string) +1];
strcpy(aPtr, string);
tPtr = new char[strlen(string2) +1];
strcpy(tPtr, string2);
yearPub = ypub;
numPages = npages;
bookType = btype;
}
void Textbook::display()
{
cout << "The name of the author is: " << *aPtr << endl;
cout << "The Title of the book is: " << *tPtr << endl;
cout << "The year it was published is: " << yearPub << endl;
cout << "The number of pages is: " << numPages << endl;
cout << "The initial of the title is: " << bookType << endl;
return;
}
void Textbook::operator=(Textbook& newbook)
{
if(aPtr != NULL) //check that it exists
delete(aPtr);// delete if neccessary
aPtr = new char[strlen(newbook.aPtr) + 1];
strcpy(aPtr, newbook.aPtr);
if(tPtr != NULL) //check that it exists
delete(tPtr); // delete if neccessary
tPtr = new char[strlen(newbook.tPtr) + 1];
strcpy(tPtr, newbook.tPtr);
yearPub = newbook.yearPub;
numPages = newbook.numPages;
bookType = newbook.bookType;
}
void main()
{
Textbook book1("sehwag", "Programming Methods", 2009, 200, 'H');
Textbook book2("Ashwin", "Security Implementation", 2011, 437, 'P');
Textbook book3;
book1.display();
book2.display();
book3.display();
book3 = book1;
book2 = book3;
book1.display();
book2.display();
book3.display();
}
im not sure if the problem lies in the default constructor but that's about the only thing i could think of, but im not sure at all on how to fix it.
Problem is with the default-parameters in the constructor.
You can't do those kind of operations with NULL-pointers.
Textbook book3;
crashes your program.
Change:
cout << "The name of the author is: " << *aPtr << endl;
cout << "The Title of the book is: " << *tPtr << endl;
to:
cout << "The name of the author is: " << aPtr << endl;
cout << "The Title of the book is: " << tPtr << endl;
Also change:
aPtr = new char[strlen(string) +1];
strcpy(aPtr, string);
to:
if (string != NULL)
{
aPtr = new char[strlen(string) +1];
strcpy(aPtr, string);
}
else
{
aPtr = new char[1];
aPtr[0] = '\0';
}
and ditto for tptr and string2.
The reason you need this checking is because you have NULL as a default value for your two string inputs, so when you call the constructor with no arguments (as is the case with book3) these strings are just NULL pointers. Calling functions such as strlen or strcat with a NULL pointer will result in an exception as you have seen.
Ideally you should not be using C-style strings with C++ - use C++ strings instead - this will help to avoid problems such as the above.

Resources