Segmentation fault(core dumped) in C++ [closed] - c++

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I'm getting this annoying error when compiling a C++ program. I looked it up and didn't find any answer. It's strange because my code is really basic and shouldn't cause any problems at all but yet it does.. I'm using Ubuntu and CODEBLOCKS IDE for the job. Here's the full code:
#include <iostream>
#include <iomanip>
using namespace std;
struct database
{
string number;
string spec_number;
char group;
float grade;
string name;
string family_name;
};
void FillDatabase(database student[], short N,short &i);
void SplitDatabase(database student[], short N,short i);
int main()
{
short number_of_students,iteration=0;
//ПРОВЕРКА:
cout << "Za kolko studenta shte vyvejdate? ";
cin >> number_of_students;
cin.ignore();
database student[number_of_students];
FillDatabase(student,number_of_students,iteration);
//МЕНЮ ЗА ИЗБОР
SplitDatabase(student, number_of_students,iteration);
return 0;
}
//--------------------------------------------------------------------------------------------------------------------------------------------
void FillDatabase(database student[], short N,short &i)
{
for(i=0;i < N;i++)
{
//ДОПЪЛНИТЕЛНИ ПРОВЕРКИ ЗА ВАЛИДНОСТ:
cout << endl << endl << "VIE VYVEJDATE ZA STUDENT NOMER " << i+1 << "." << endl;
cout << endl << "Vyvedete fakulteten nomer na studenta...";
cin >> student[i].number;
cout << endl << "Vyvedete kod na specialnostta 52(E) ili 61(AIUT)...";
cin >> student[i].spec_number;
cout << endl << "Vyvedete grupa na studenta...";
cin >> student[i].group;
cout << endl << "Vyvedete sreden uspeh ot semestyra...";
cin >> student[i].grade;
cout << endl << "Vyvedete ime na studenta...";
cin >> student[i].name;
cout << endl << "Vyvedete familiya na studenta...";
cin >> student[i].family_name;
cin.ignore();
}
}
void SplitDatabase(database student[], short N,short i)
{
short count_of_E(0);
for(i=0;i < N;i++)if(student[i].spec_number=="52")count_of_E++;
database E_student[count_of_E];
database AIUT_student[N-count_of_E];
for(i=0;i < N;i++)
{
if(student[i].spec_number=="52")
{
E_student[i].number = student[i].number;
E_student[i].spec_number = student[i].spec_number;
E_student[i].group = student[i].group;
E_student[i].grade = student[i].grade;
E_student[i].name = student[i].name;
E_student[i].family_name = student[i].family_name;
}
else
{
AIUT_student[i].number = student[i].number;
AIUT_student[i].spec_number = student[i].spec_number;
AIUT_student[i].group = student[i].group;
AIUT_student[i].grade = student[i].grade;
AIUT_student[i].name = student[i].name;
AIUT_student[i].family_name = student[i].family_name;
}
}
cout << endl << endl << endl << left << "E" << setw(50) << right << "AIUT" << endl << endl;
for(i=0;i < N;i++)
{
cout << left << E_student[i].name << endl;
cout << left << E_student[i].family_name << endl;
cout << left << E_student[i].number << endl;
cout << left << E_student[i].group << endl;
cout << left << E_student[i].grade << endl << endl;
cout << setw(50) << right << AIUT_student[i].name << endl;
cout << setw(50) << right << AIUT_student[i].family_name << endl;
cout << setw(50) << right << AIUT_student[i].number << endl;
cout << setw(50) << right << AIUT_student[i].group << endl;
cout << setw(50) << right << AIUT_student[i].grade << endl;
}
}
/*
string number;
string spec_number;
char group;
float grade;
string name;
string family_name;
61462166 52 2 5.50
*/
I hope that someone lends me a hand here, I'm frustrated....

The problem (when running, not compiling) is in lines like these:
E_student[i].number = student[i].number;
E_student[i].spec_number = student[i].spec_number;
//...
AIUT_student[i].number = student[i].number;
AIUT_student[i].spec_number = student[i].spec_number;
You're looping over student from 0..N-1 -- however E_student and AIUT_student are smaller than N. If N == 4, and E_student and AIUT_student are both of size 2, the last time through the loop you're assigning to AIUT_student[3] or E_student[3] -- past the end of the respective arrays.
You need to keep separate indices for each array:
int ei = 0, ai = 0;
for (i = 0; i < N; i++)
{
if (student[i].spec_number == "52")
{
E_student[ei].number = student[i].number;
E_student[ei].spec_number = student[i].spec_number;
E_student[ei].group = student[i].group;
E_student[ei].grade = student[i].grade;
E_student[ei].name = student[i].name;
E_student[ei].family_name = student[i].family_name;
++ei;
}
else
{
AIUT_student[ai].number = student[i].number;
AIUT_student[ai].spec_number = student[i].spec_number;
AIUT_student[ai].group = student[i].group;
AIUT_student[ai].grade = student[i].grade;
AIUT_student[ai].name = student[i].name;
AIUT_student[ai].family_name = student[i].family_name;
++ai;
}
}

your problem is that you adress the both partial arrays AIUT_student and E_student with the index of the whole student array. this will result in an access violation latest at N/2+1

Related

error: request for member ‘find’ in ‘sentence’, which is of non-class type ‘char*’ [duplicate]

This question already has answers here:
What is array to pointer decay?
(11 answers)
Array Size Member Function Compile Error
(3 answers)
Closed 15 days ago.
I get an error saying "'find' in ‘sentence’, which is of non-class type ‘char*’".
So the instruction was to enter a string and identify which in the string is a noun, pronoun, adjectives and a linking verb. Everything seems to work except the ".find" part. I really need help.
(btw, nvm the other unecessary things. I made the code shorter to put focus more on the problem)
#include <iostream>
#include <cstring>
#include <cctype>
#include <string>
using namespace std;
string student_id, first_name, last_name, middle_name, suffix;
char sentence[100][100];
char history1[100][100];
int h = 0, s= 0;
char noun[20][20] = {"Ervin", "Rafi", "Peco", "luis", "Edgar", "Benj", "Rias", "Aki", "Naruto", "Jhay", "Josh", "Jopay", "Sasuke", "Joshua", "Trump", "Benjo", "Alice", "Janelle", "Samantha", "Jairah"};
char pronoun[20][20] = {"he", "she", "i", "it", "you"};
char verb[20][20] = {"is", "was", "are", "as", "am"};
char adj[20][20] = {"big", "small", "racist", "fat", "Funny", "gay", "black", "white", "rainbow", "tiny", "smart", "bi", "pan", "stupid", "idiot", "buang", "retard", "gaymer", "special", "talented"};
void display_dictionary();
void open_checker();
bool hasElement(char sentence[], char elements[], int size);
int main()
{
int option;
cout << endl << "Account Menu" <<endl;
cout << "[1] Open checker" << endl;
cout << "[2] Display Dictionary" << endl;
cout << "Option: ";
cin >> option;
switch (option){
case 1: open_checker(); break;
case 2: display_dictionary(); break;
}
return 0;
}
void display_dictionary(){
cout << "List of Nouns: ( ";
for (int n = 0; n < 20; n++){
cout << noun[n] << " " ;
}
cout << ")" << endl << endl;
cout << "List of Pronouns: ( ";
for (int n = 0; n < 5; n++){
cout << pronoun[n] << " " ;
}
cout << ")" << endl << endl;
cout << "List of Linking Verbs: ( ";
for (int n = 0; n < 5; n++){
cout << verb[n] << " " ;
}
cout << ")" << endl << endl;
cout << "List of Adjectives: ( ";
for (int n = 0; n < 20; n++){
cout << adj[n] << " " ;
}
cout << ")" << endl << endl;
}
void open_checker(){
int option;
cout << "Enter Sentence: ";
cin.ignore();
cin.get(sentence[s], 100);
if (hasElement(sentence[s], noun[s], 20)){
cout << " Is a noun." << endl;
}
if (hasElement(sentence[s], adj[s], 20)){
cout << " Is an Adjectives." << endl;
}
if (hasElement(sentence[s], verb[s], 5)){
cout << " Is a Linking Verb." << endl;
}
if (hasElement(sentence[s], pronoun[s], 5)) {
cout << " Is a Linking Verb." << endl;
}
else {
cout << "Sentence does not have all required elements." << endl;
}
strcpy(history1[h], sentence[s]);
history1[h][h] = sentence[s][s];
h++;
s++;
cout << "Type 1 to go back: ";
cin >> option;
if (option == 1){
main();
}
}
bool hasElement(char sentence[], char elements[], int size){
for (int i = 0; i < size; i++) {
if (sentence.find(elements[i]) != string::npos) {
cout << elements[i];
return true;
}
}
return false;
}```

Why am I getting strange values for integer in this example of derived class (C++)

I was given a task to write an example of derived class. But In my program, something strange is happening with the roll numbers.
Also, when this program is compiled in g++.
When I used char [] and gets() to store the strings and input values into them, it didn't allow me to enter the value for collname.
When I use string and cin, I get some strange values while asking for marks.(Check the attached image).
#include<iostream>
#include<conio.h>
using namespace std;
class uni
{
private:
int rollno[100], i, flag;
int intermarks[100];;
int theorymarks[100];
void settheorymarks();
protected:
int numstud;
void setintermarks();
void issurno();
public:
void prepres();
void showres(string collname);
};
class college : public uni
{
private:
string collname;
public:
college(int N)
{
numstud = N;
issurno();
}
void enter_marks();
void disp();
};
void uni::issurno()
{
for (i = 0; i < numstud; i++)
rollno[i] = 1024+i;
cout << "Roll numbers issued!" << endl;
}
void uni::settheorymarks()
{
cout << "Enter theory marks for: " << endl ;
for(i = 0; i < numstud; i++)
{
cout << i+1 << ".) Roll number: " << rollno << " : ";
cin >> theorymarks[i];
}
cout << endl << endl << "Theory marks recorded!" << endl;
}
void uni::setintermarks()
{
cout << "Enter inter marks for: " << endl ;
for(i = 0; i < numstud; i++)
{
cout << i+1 << ".) Roll number: " << rollno << " : ";
cin >> intermarks[i];
}
cout << endl << endl << "Internal marks recorded!" << endl;
}
void uni::prepres()
{
settheorymarks();
}
void uni::showres(string colnam)
{
cout << "College: " << colnam << endl;
cout << "__________________________Result___________________________" << endl;
cout << "s. No.\tRoll no\tInternal\tTheory" << endl;
for(i = 0; i < numstud; i++)
cout << i+1 << "\t" << rollno[i] << '\t' << intermarks[i] << "\t" << theorymarks[i] << endl;
cout << endl << "End of result!" << endl;
}
void college::disp()
{
showres(collname);
}
void college::enter_marks()
{
cout << "Enter the college name: ";
cin >> collname;
setintermarks();
prepres();
}
int main()
{
int n;
cout << "Enter number of stufents: ";
cin >> n;
college c(n);
c.enter_marks();
c.disp();
return 0;
}
I feel that I've made a stupid mistake somewhere.
PS: In school, I was forced to use turbo C++ (One of the oldest compilers).
You just forget the [i] after rollno in the two lines like:
cout << i+1 << ".) Roll number: " << rollno[i] << " : ";
There are a few other things I would ask you to improve if I was your supervisor/teacher:
always use expressive variable and method names. Avoid any abbreviations.
why is there the arbitray offset of 1024 in rollno? If this is just "obfuscation" remove it...
Unclear: why is setintermarks called inside enter_marks but settheorymarks in prepres ?
typo in "stufents"

Number that is inputted is not the same as displayed by the code, using array in a struct, why?

I'm making a code where users can type in numbers that will be stored in an array which is inside a struct. However, sometimes, the value that I input is not the same as the one that is stored/display. This problem is not very consistent. For example, when I input 10, it could either be shown as: 6384644, 6382852, or actually 10. I am very confused for this, I've tried changing the array data type using int, long, and double, but to no avail.
#include <iostream>
#include <iomanip>
using namespace std;
int main () {
int n,x,totalAge = 0,maxAge = 0,avgAge = 0,maxGoals = 0,bestPlayer = 0, oldest = 0;
cout << "Input the number of players: ";
cin >> n;
cout << "Input the number of games: ";
cin >> x;
struct players {
string name;
string currTeam;
int shirtn;
int age;
float height;
float weight;
int totalGoals;
long goals[];
};
players goals[x];
players playerList[n];
for (int i = 0; i < n; i++) {
cout << "Player " << (i+1) << endl;
cout << "Input player's name: ";
cin.ignore();
getline (cin, playerList[i].name);
cout << "Input player's current team: ";
getline (cin, playerList[i].currTeam);
cout << "Input player's shirt number: ";
cin >> playerList[i].shirtn;
cout << "Input player's age: ";
cin >> playerList[i].age;
cout << "Input player's height (m): ";
cin >> playerList[i].height;
cout << "Input player's weight (kg): ";
cin >> playerList[i].weight;
cout << endl;
for (int a = 0; a < x; a++) {
playerList[i].goals[a] = 0;
playerList[i].totalGoals = 0;
}
for (int a = 0; a < x; a++) {
cout << "Game " << (a+1) << "'s number of goals: ";
cin >> playerList[i].goals[a];
playerList[i].totalGoals += playerList[i].goals[a];
}
if (playerList[i].totalGoals > maxGoals) {
maxGoals = playerList[i].totalGoals;
bestPlayer = i;
}
if (playerList[i].age > maxAge) {
maxAge = playerList[i].age;
oldest = i;
}
totalAge += playerList[i].age;
cout << endl;
}
cout << endl;
for (int i = 0; i < n; i++) {
cout << playerList[i].name << endl;
cout << "--------------------" << endl;
cout << "Current team: " << playerList[i].currTeam << endl;
cout << "Shirt Number: " << playerList[i].shirtn << endl;
cout << "Age: " << playerList[i].age << endl;
cout << "Height: " << playerList[i].height << " m" << endl;
cout << "Weight: " << playerList[i].weight << " kg" << endl;
cout << endl;
for (int a = 0; a < x; a++) {
cout << "Game " << (a+1) << "'s number of goals: " << playerList[i].goals[a] << endl;
}
cout << endl << endl;
}
avgAge = totalAge / n;
cout << "Average age of players: " << avgAge << endl;
cout << "Oldest Player: " << playerList[oldest].name << " (" << maxAge << ") ";
cout << "Player who got the most goals: " << playerList[bestPlayer].name << ", shirt number: " << playerList[bestPlayer].shirtn << ". With total goals of: " << playerList[bestPlayer].totalGoals << endl;
}
Your code's corrected version would be this:
#include<string>
#include<iostream>
#include<vector> //for vector
using namespace std;
struct players {
string name;
string currTeam;
int shirtn;
int age;
float height;
float weight;
int totalGoals;
vector<long> goals; //used vector instead of array
};
int main () {
int N, X;
int totalAge = 0, maxAge = 0, avgAge = 0, maxGoals = 0, bestPlayer = 0, oldest = 0;
cout << "Input the number of players: ";
cin >> N;
cout << "Input the number of games: ";
cin >> X;
players player[X];
for (int i = 0; i < N; i++) {
cout << "Player " << (i+1) << endl;
cout << "Input player's name: ";
cin>>player[i].name;
cout << "Input player's current team: ";
cin>>player[i].currTeam;
cout << "Input player's shirt number: ";
cin >> player[i].shirtn;
cout << "Input player's age: ";
cin >> player[i].age;
cout << "Input player's height (m): ";
cin >> player[i].height;
cout << "Input player's weight (kg): ";
cin >> player[i].weight;
cout << endl;
player[i].totalGoals = 0;
for (int a = 0; a < X; a++) {
long G;
cout << "Game " << (a+1) << "'s number of goals: ";
cin >> G;
player[i].goals.push_back(G);
player[i].totalGoals += G;
}
if (player[i].totalGoals > maxGoals) {
maxGoals = player[i].totalGoals;
bestPlayer = i;
}
if (player[i].age > maxAge) {
maxAge = player[i].age;
oldest = i;
}
totalAge += player[i].age;
cout << endl;
}
cout << endl;
for (int i = 0; i < N; i++) {
cout << player[i].name << endl;
cout << "--------------------" << endl;
cout << "Current team: " << player[i].currTeam << endl;
cout << "Shirt Number: " << player[i].shirtn << endl;
cout << "Age: " << player[i].age << endl;
cout << "Height: " << player[i].height << " m" << endl;
cout << "Weight: " << player[i].weight << " kg" << endl;
cout << endl;
for (int a = 0; a < X; a++) {
cout << "Game " << (a+1) << "'s number of goals: " << player[i].goals[a] << endl;
}
cout << endl << endl;
}
avgAge = totalAge / N;
cout << "Average age of players: " << avgAge << endl;
cout << "Oldest Player: " << player[oldest].name << " (" << maxAge << ") ";
cout << "Player who got the most goals: " << player[bestPlayer].name << ", shirt number: " << player[bestPlayer].shirtn << ". With total goals of: " << player[bestPlayer].totalGoals << endl;
return 0;
}

(C++) Code seems to be operating off the wrong loop...?

I've been playing around with some code in my down-time from my degree and I've nested a do{}while() loop inside another one but the problem I'm having is that the code keeps going until the last van is full, even after the number of parcels has been fulfilled...
The code's below. If someone could take a look at it and tell me what I've done wrong that'd be awesome. Bare in mind I've only really been coding in C++ for about a month so I've still got hella lot to learn..
#include <iostream>
using namespace std;
char cBeltFull;
int iVanCount, iParcelCount, iParcelLoaded;
float fHeaviestVanWeight, fParcelWeight, fCurrentPayload, fVanCapacity;
char cExit = 'N';
int main() {
iVanCount = 1;
iParcelLoaded = 1;
fHeaviestWeight = 0;
fVanCapacity = 410;
do {
//Get the number of parcels to dispatch
cout << "How many parcels need sending?" << endl;
cin >> iParcelCount;
do {
fCurrentPayload = 0;
do {
//Get parcel weight
cout << endl << endl << endl << "What is the weight the parcel " << iParcelLoaded << "?";
cin >> fParcelWeight;
//'Load' the parcel
cout << endl << endl << "Parcel loaded";
iParcelLoaded ++;
//Update the payload
fCurrentPayload = fCurrentPayload + fParcelWeight;
} while ((fCurrentPayload + fParcelWeight)) < fVanCapacity)
//Dispatch the van
cout << endl << endl << "Van dispatched.";
//Update the van count
iVanCount ++;
if (fCurrentPayload > fHeaviestVanWeight) {
//Update the heaviest weight
fHeaviestVanWeight = fCurrentPayload;
}
} while (iParcelLoaded <= iParcelCount);
cout << endl << endl << endl << "Vans dispatched: " << iVanCout;
cout << endl << endl << "Weight of heaviest van: " << fHeaviestWeight;
cout << endl << endl << endl << "Exit? Y for YES or N for NO." << endl;
cin >> cExit;
} while (cExit == 'N');
}
Change this
} while (((fCurrentPayload + fParcelWeight)) < fVanCapacity);
to this
} while (((fCurrentPayload + fParcelWeight)) < fVanCapacity
&& iParcelLoaded < iParcelCount);
That way you will load as many items the user inputs. You code contains many syntax errors.
I corrected them for you, but please be more careful next time you post.
#include <iostream>
using namespace std;
char cBeltFull;
int iVanCount, iParcelCount, iParcelLoaded;
float fHeaviestVanWeight, fParcelWeight, fCurrentPayload, fVanCapacity;
char cExit = 'N';
int main() {
iVanCount = 1;
iParcelLoaded = 1;
fHeaviestVanWeight = 0;
fVanCapacity = 410;
do {
//Get the number of parcels to dispatch
cout << "How many parcels need sending?" << endl;
cin >> iParcelCount;
do {
fCurrentPayload = 0;
do {
//Get parcel weight
cout << endl << endl << endl << "What is the weight the parcel " << iParcelLoaded << "?";
cin >> fParcelWeight;
//'Load' the parcel
cout << endl << endl << "Parcel loaded";
iParcelLoaded ++;
//Update the payload
fCurrentPayload = fCurrentPayload + fParcelWeight;
} while (((fCurrentPayload + fParcelWeight)) < fVanCapacity && iParcelLoaded < iParcelCount);
//Dispatch the van
cout << endl << endl << "Van dispatched.";
//Update the van count
iVanCount ++;
if (fCurrentPayload > fHeaviestVanWeight) {
//Update the heaviest weight
fHeaviestVanWeight = fCurrentPayload;
}
} while (iParcelLoaded <= iParcelCount);
cout << endl << endl << endl << "Vans dispatched: " << iVanCount;
cout << endl << endl << "Weight of heaviest van: " << fHeaviestVanWeight;
cout << endl << endl << endl << "Exit? Y for YES or N for NO." << endl;
cin >> cExit;
} while (cExit == 'N');
}

Why is my string getting modified inside this code? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am taking an integer as input in a string that is string s. I want to write integer in s in the form of "-"'s and "|". I am sure that my logic is right. The problem is string s is automatically getting modified inside the code. When I initially print the string s, it returns complete 12345 (My input is "2 12345") but when I tries to print it afterwards it is getting truncated or something. How do I resolve this?
#include <iostream>
#include <string>
using namespace std;
int main(){
int n;
std::string s;
cin >> n;
cin >> s;
cout << s.at(3) <<endl;
while(n!=0){
for (int l=0;l<3+2*n;l++){
// for (int i=0;i<s.length();i++){
if (l==0){
for (int j=0;j<s.length();j++){
if (s.at(j)=='1'||s.at(j)=='4'){
cout << " ";
for (int k=0;k<n;k++){
cout << " ";
}
cout << " ";
}
else if (s.at(j)=='0'||s.at(j)=='2'||s.at(j)=='3'||s.at(j)=='5'||s.at(j)=='6'||s.at(j)=='7'||s.at(j)=='8'||s.at(j)=='9'){
cout << " ";
for (int k=0;k<n;k++){
cout << "-";
}
cout << " ";
}
cout << " ";
}
}
else if (l==n+1){
for (int j=0;j<s.length();j++){
if (s.at(j)=='1'||s.at(j)=='7'||s.at(j)=='0'){
cout << " ";
for (int k=0;k<n;k++){
cout << " ";
}
cout << " ";
}
else if (s.at(j)=='2'||s.at(j)=='3'||s.at(j)=='4'||s.at(j)=='5'||s.at(j)=='6'||s.at(j)=='8'||s.at(j)=='9'){
cout << " ";
for (int k=0;k<n;k++){
cout << "-";
}
cout << " ";
}
cout << " ";
}
}
else if (l==2*n+2){
for (int j=0;j<s.length();j++){
if (s.at(j)=='1'||s.at(j)=='4'||s.at(j)=='7'){
cout << " ";
for (int k=0;k<n;k++){
cout << " ";
}
cout << " ";
}
else if (s.at(j)=='0'||s.at(j)=='2'||s.at(j)=='3'||s.at(j)=='6'||s.at(j)=='8'||s.at(j)=='9'||s.at(j)=='5'){
cout << " ";
for (int k=0;k<n;k++){
cout << "-";
}
cout << " ";
}
cout << " ";
}
}
else if ((l>0) && (l<n+1)){
for (int j=0;j<s.length();j++){
if (s.at(j)=='1'||s.at(j)=='2'||s.at(j)=='3'||s.at(j)=='7'){
cout << " ";
for (int k=0;k<n;k++){
cout << " ";
}
cout << "|";
}
else if(s.at(j)='4'||s.at(j)=='8'||s.at(j)=='9'||s.at(j)=='0') {
cout << "|";
cout << "s "<< s<< endl;
cout << "check 2";
for (int k=0;k<n;k++){
cout << " ";
}
cout << "|";
}
else if(s.at(j)=='5'||s.at(j)=='6'){
cout << "|";
cout << "check";
for (int k=0;k<n;k++){
cout <<" ";
}
cout << " ";
}
cout << " ";
}
}
else if ((l>n+1) && (l<2*n+2)){
for (int j=0;j<s.length();j++){
if (s.at(j)=='1'||s.at(j)=='3'||s.at(j)=='5'||s.at(j)=='7'||s.at(j)=='9'||s.at(j)=='4'){
cout << " ";
for (int k=0;k<n;k++){
cout << " ";
}
cout << "|";
}
else if(s.at(j)='6'||s.at(j)=='8'||s.at(j)=='0') {
cout << "|";
for (int k=0;k<n;k++){
cout << " ";
}
cout << "|";
}
else if(s.at(j)=='2'){
cout << "|";
for (int k=0;k<n;k++){
cout <<" ";
}
cout << " ";
}
cout << " ";
}
}
cout << s << endl;
cout << endl;
}
cin >> n;
cin >> s;
}
}
else if(s.at(j)='4'||
You forgot an = sign.
A quick look reveals that s.at(j)='6' and s.at(j)='4' modify your string. You can avoid this by using Yoda Conditions.
There are compilers that may warn you about an assignment, here. (See comments).
Also see Mats Petersson's answer, which I consider to be the best adivce: Use const objects, references or pointers to const memory if you do not want to alter contents in a certain situation.
If you'd use a function that takes a std::string const &s for it's argument, doing the job, you'd end up having
cleaner code structure and
compiler errors upon alteration of your argument s.
If you have a situation where you want to avoid unintentionally modifying a variable (for example a string), you can either break out the relevant code into a function and pass a const string& x to the function, or you can create a local const reference. In your code, one could do:
std::string ms;
cin >> n;
cin >> ms;
const &std::string s = ms;
Now you will get an error any time you try to make changes to s, because it's a const.