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

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.

Related

Error: expected primary-expression before ']' token. Need assitance with function definition and calling

This is my first semester of computer science, and I need help with my first project. So far, it's still a mess, and I am mainly doing test cases to ensure basic things like my functions work.
The goal of the project is to ask the user how many robots they want to make, name them, then they can use the robot's name (their unique identifier) to move the robots along an X-Y axis, which is really just the program adding or subtracting to a .Xvalue or .Yvalue.
My MenuFunction works, but I am having trouble with the MoveFunction. Basically, I want the MoveFunction to ask the user which robot they want to use, go through the RobotArray, and print the Robot's name once found.
Again, this is just a test case so I can better understand the coding. Right now, I am getting two errors:
main.cpp:42:33: error: expected primary-expression before ‘]’ token
42 | string MoveFunction(RobotArray[], robotName, NumberOfRobots);
main.cpp:62:16: error: no match for call to ‘(std::string {aka std::__cxx11::basic_string}) ()’
62 | MoveFunction();
I don't know what to do for the first error, but I think the latter is due to my not having any objects in the function call, but I wouldn't know what to put in there anyway.
My complete code is below:
#include <iostream>
using namespace std;
struct userRobot
{
string name;
int Xvalue = 0;
int Yvalue = 0;
};
void MenuFunction()
{
cout << "Welcome to MultiRobo Guider." << endl;
cout << "Please select:" << endl;
cout << "m - move" << endl << "d - distance" << endl << "q - quit" << endl << endl;
}
int main()
{
int NumberOfRobots;
string robotName;
cout << "Enter the number of robots" << endl;
cin >> NumberOfRobots;
cout << endl << "Enter their name(s)" << endl;
userRobot RobotArray[NumberOfRobots];
for (int i = 0; i < NumberOfRobots; i++)
{
cin >> robotName;
RobotArray[i].name = robotName;
}
cout << endl;
for (int j = 0; j < NumberOfRobots; j++)
{
cout << RobotArray[j].name << "'s position is ";
cout << "(" << RobotArray[j].Xvalue << "," << RobotArray[j].Yvalue << ")" << endl << endl;
}
string MoveFunction(RobotArray[], robotName, NumberOfRobots);
{
cin >> robotName;
for (int k = 0; k < NumberOfRobots; k++)
{
if (robotName == RobotArray[k].name)
{
cout << RobotArray[k].name;
}
}
}
MenuFunction();
char input;
cin >> input;
if (input == 'm')
{
cout << "Which robot would you like to move?";
MoveFunction();
}
else if (input == 'd')
{
cout << "distance";
}
else if (input == 'q')
{
cout << "quit";
}
}
First off, userRobot RobotArray[NumberOfRobots]; is a variable-length array, which is a non-standard extension supported by only a few compilers. To create an array whose size is not known until runtime, you should use new[] instead, or better std::vector.
That said, your main issue is that you are trying to define your MoveFunction() function inside of your main() function, which is not allowed. But, even if it were, you are not declaring it correctly. It has an erroneous ; on it. And RobotArray, robotName, and NumberOfRobots are not types, but variables. Like a variable, a function parameter always starts with a type. There are no untyped parameters/variables in C++.
You need to either:
move MoveFunction() outside of main(), and fix its declaration to use proper types, eg:
#include <iostream>
#include <vector>
#include <string>
using namespace std;
struct userRobot
{
string name;
int Xvalue = 0;
int Yvalue = 0;
};
void MenuFunction()
{
cout << "Welcome to MultiRobo Guider." << endl;
cout << "Please select:" << endl;
cout << "m - move" << endl << "d - distance" << endl << "q - quit" << endl << endl;
}
void MoveFunction(userRobot RobotArray[], int NumberOfRobots)
{
cout << "Which robot would you like to move?";
string robotName;
cin >> robotName;
for (int k = 0; k < NumberOfRobots; k++)
{
if (robotName == RobotArray[k].name)
{
cout << RobotArray[k].name << endl;
return;
}
}
cout "Robot not found" << endl;
}
int main()
{
cout << "Enter the number of robots" << endl;
int NumberOfRobots;
cin >> NumberOfRobots;
vector<userRobot> RobotArray(NumberOfRobots);
cout << endl << "Enter their name(s)" << endl;
for (int i = 0; i < NumberOfRobots; i++)
{
cin >> RobotArray[i].name;
}
cout << endl;
for (int j = 0; j < NumberOfRobots; j++)
{
cout << RobotArray[j].name << "'s position is ";
cout << "(" << RobotArray[j].Xvalue << "," << RobotArray[j].Yvalue << ")" << endl << endl;
}
MenuFunction();
char input;
cin >> input;
if (input == 'm')
{
MoveFunction(RobotArray.data(), NumberOfRobots);
}
else if (input == 'd')
{
cout << "distance";
}
else if (input == 'q')
{
cout << "quit";
}
}
or change MoveFunction() into a lambda, eg:
#include <iostream>
#include <vector>
#include <string>
using namespace std;
struct userRobot
{
string name;
int Xvalue = 0;
int Yvalue = 0;
};
void MenuFunction()
{
cout << "Welcome to MultiRobo Guider." << endl;
cout << "Please select:" << endl;
cout << "m - move" << endl << "d - distance" << endl << "q - quit" << endl << endl;
}
int main()
{
cout << "Enter the number of robots" << endl;
int NumberOfRobots;
cin >> NumberOfRobots;
vector<userRobot> RobotArray(NumberOfRobots);
cout << endl << "Enter their name(s)" << endl;
for (int i = 0; i < NumberOfRobots; i++)
{
cin >> RobotArray[i].name;
}
cout << endl;
for (int j = 0; j < NumberOfRobots; j++)
{
cout << RobotArray[j].name << "'s position is ";
cout << "(" << RobotArray[j].Xvalue << "," << RobotArray[j].Yvalue << ")" << endl << endl;
}
auto MoveFunction = [&]{
cout << "Which robot would you like to move?";
string robotName;
cin >> robotName;
for (int k = 0; k < NumberOfRobots; k++)
{
if (robotName == RobotArray[k].name)
{
cout << RobotArray[k].name << endl;
return;
}
}
cout "Robot not found" << endl;
};
MenuFunction();
char input;
cin >> input;
if (input == 'm')
{
MoveFunction();
}
else if (input == 'd')
{
cout << "distance";
}
else if (input == 'q')
{
cout << "quit";
}
}
You have a semi-colon at the end of MoveFunction() definition, In C++ when you define a function the name of the function does not end with a semi-colon.

Keep receiving this error main.cpp:9:91: error: no match for ‘operator<<’ (operand types are ‘std::basic_ostream’ and ‘const std::vector’

Sorry in advance if this is lengthy. the problem is within line 9. (where the 2nd cout is.) apparently, but i'm new to this so I can't identify exactly what the issue is.
#include <iostream>
#include <vector>
using namespace std;
void outputRoster(const vector<int> &jersey, const vector<int> &ratings) {
cout << "ROSTER" << endl;
for (int i =1; i < jersey.size(); ++i) {
cout << "Player " << i << " -- Jersey number: " << jersey.at(i-1) << ", Rating: " << ratings << endl;
}
cout << endl;
}
void addPlayer(vector<int> &jersey, vector<int> &ratings) {
int num;
cout << "Enter another player's jersey number: ";
cin >> num;
jersey.push_back(num);
cout << "Enter another player's ratings: ";
cin >> num;
cout << endl;
ratings.push_back(num);
}
void removePlayer(vector<int> &jersey, vector<int> &ratings) {
int num;
cout << "Enter a jersey number: ";
cin >> num;
cout << endl;
for (int i = 0; i < jersey.size(); ++i){
if (jersey.at(i)==num){
jersey.erase(jersey.begin()+i);
ratings.erase(ratings.begin()+i);
break;
}
}
}
void updatePlayerRating(const vector<int> &jersey, vector<int> &ratings){
int num;
cout << "Enter a jersey number: " << endl;
cin >> num;
for (int i = 0; i < jersey.size(); ++i){
if(jersey.at(i) == num){
cout << "Enter a new rating for player: ";
cin >> num;
cout << endl;
ratings.at(i) = num;
}
}
}
void outputPlayersAboveRating(const vector<int> &jersey, const vector<int> &ratings) {
int num;
cout << "Enter a rating: ";
cin >> num;
cout << endl;
cout << "ABOVE " << num << endl;
for (int i = 0; i < ratings.size(); ++i){
if (ratings.at(i) > num) {
cout << "Player " << i+1 << " -- Jersey number: " << jersey.at(i) << ", Rating: " << ratings.at(i);
}
}
cout << endl;
}
int main() {
vector<int> jersey;
vector<int> ratings;
for (int i = 0; i < 5; ++i) {
int num;
cout << "Enter player " << i+1 << "'s jersey number:";
cin >> num;
jersey.push_back(num);
cout << "Enter player " << i+1 << "'s ratings:";
cin >> num;
ratings.push_back(num);
cout << endl;
cout << endl;
}
outputRoster(jersey, ratings);
char inp;
while(true) {
cout << "MENU" << endl;
cout << "a - Add player" << endl;
cout << "d - Remove player" << endl;
cout << "u - Update player rating" << endl;
cout << "r - Output players above a rating" << endl;
cout << "o - Output roster" << endl;
cout << "q - Quit" << endl;
cout << "Choose an option: ";
cin >> inp;
cout << endl;
if (inp == 'a') {
addPlayer(jersey, ratings);
}
else if (inp == 'd') {
removePlayer(jersey, ratings);
}
else if (inp == 'u') {
updatePlayerRating(jersey, ratings);
}
else if (inp == 'r') {
outputPlayersAboveRating(jersey, ratings);
}
else if (inp == 'o') {
outputRoster(jersey, ratings);
}
else if (inp == 'q') {
return 0;
}
}
return 0;
}
Any and all help is appreciated also if possible, could you explain how to avoid an error like this in the future.
Thanks in advance.
You cannot print ratings directly in
cout ... << ratings ... because std::vector doesn't have an operator overload for printing. Rather, you have to print out an element inside that vector, so change it to cout ... << ratings[i] ..., which I'm assuming is your desired effect.
This is exactly what the compiler error is telling you. std::vector doesn't have an overload (no operator<< match).

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"

Creating and clearing an array of structures

I've been trying to write a short program allowing the user to add entries to a "database", listing the entries they have put in, and the ability to clear all the entries without ending the program. Here's what i've got
#include <cstdlib>
#include <iostream>
#include <string>
using namespace std;
struct BIRTH
{int month; int year;};
struct ID
{string name; bool vip; float score;
struct BIRTH date;} ;
int main(int argc, char** argv) {
ID **ptrarr;
ptrarr = new ID * [10];
for (int r=0; r<10; r++)
{ptrarr[r] = new ID[1] ;}
int counter = 0;
while(counter<100){
cout << "Type add to create a new entry" << endl;
cout << "Type list to see all entries" << endl;
cout << "Type clear to delete all entries" << endl;
cout << "Type exit to terminate" << endl;
string command = "0";
getline (cin,command);
if(command=="add")
{
cout << "Enter name" << endl;
getline (cin,ptrarr[counter][1].name);
cout << "VIP? 1 for yes, 0 for no" << endl;
cin >> ptrarr[counter][1].vip;
cout << "Enter score" << endl;
cin >> ptrarr[counter][1].score;
cout << "Month of birth" << endl;
cin >> ptrarr[counter][1].date.month;
cout << "Year of birth" << endl;
cin >> ptrarr[counter][1].date.year;
counter++;
}
else if(command=="list")
{
for (int i=0; i<counter; i++)
{int n=i+1;
cout << n << " "
<< ptrarr[i][1].name << " ";
if (ptrarr[i][1].vip)
{cout << "VIP ";}
cout << "Score: " << ptrarr[i][1].score << " "
<< "Born: " << ptrarr[i][1].date.month << "/" << ptrarr[i][1].date.year << endl;
}
}
else if(command=="clear")
{delete[] ptrarr;
cout << "Entries cleared" << endl;}
else if(command=="exit")
{return 0;}
else
cout << "try again" << endl;
}
return 0;
}
Now here's the deal: the following code successfully compiles, but when I type in the "add" command, the program crashes (achievement unlocked, didn't think it's possible to obtain with such a short code). The most important thing is that the array is made of a multitype structure and that the "clear" command wipes out all the entries in the array.
NOTE: I understand that there are thousand better ways to write this piece of code, but I'm writing it to practice the things I have covered so far regarding C++. So unless it is absolutely necessary for the code to run, please do not introduce any new gimmicks =)
Replace all ptrarr[counter][1] with ptrarr[counter][0] fixes the problem.
Further advices:
I. This code has redundancy:
ID **ptrarr;
ptrarr = new ID * [10];
for (int r=0; r<10; r++)
{ptrarr[r] = new ID[1] ;}
Replace it with:
ID *ptrarr;
ptrarr = new ID [10];
Then you do not need extra [0] at the end of each ptrarr[counter]
II. functions make your code more readable:
if(command=="add")
add();
else if(command=="list")
list();
else if(command=="clear")
clear();
else if(command=="exit")
return 0;
else
cout << "try again" << endl;
Then decisions are made in a smaller area (Good practice for large programs.)
III. There is another mistake in your code:
else if(command=="clear")
{delete[] ptrarr;
cout << "Entries cleared" << endl;}
Here you should reset the counter. Also if you regard my point (I) this part is fine. Otherwise, if you use new with a for loop, I am afraid that you need to delete with a for loop too. Merely removing the root of the array tree brings you memory leak!
Also, if you cleared the list by delete, wont you need to store data in the list anymore? Using delete in linked lists is a good idea, but it does not apply here. Just reseting the counter does the job and it does not show IDs in the list anymore. The for inside the list does only count up to the counter.
If you exit the program don't you free the memory?
I say
delete [] ptrarr;
is good for being at exit.
You are creating an an array of pointers, each one of which points to one element:
ptrarr[r] = new ID[1] ;
The maximum index that you can use with ptrarr[r] is 0. Since you are using ptrarr[counter][1], you are accessing memory that is out of bounds. This leads to undefined behavior. Crashing is one such undefined behavior.
There are other issues with your code that you may want to fix.
More out of bounds memory access
You are using:
int counter = 0;
while(counter<100){
...
getline (cin,ptrarr[counter][1].name);
That is again going to lead to undefined behavior if counter > 10 since you allocated only 10 pointers for ptrarr.
Deleting the contents
You are using:
else if(command=="clear")
{
delete[] ptrarr;
cout << "Entries cleared" << endl;
}
There are couple of problems with this:
You have memory leak. You never call delete [] on what ptrarr[0] - ptrarr[9] point to. You'll have to use:
else if(command=="clear")
{
for ( int i = 0; i < 10; ++i )
{
delete [] ptrarr[i];
}
delete[] ptrarr;
cout << "Entries cleared" << endl;
}
Remember that every allocation must have a corresponding deallocation. Otherwise, you are leaking memory.
Once you call delete [] ptrarr;, it points to dangling memory. I don't see any code that reallocates memory for ptrarr while you continue to use it.
You need to reallocate memory and reset counter to 0 when the user chooses "clear".
My suggestion
You don't two levels of pointers. You just need something like:
int const MAX_ITEMS = 100;
ID* IDarr = new ID[MAX_ITEMS];
Instead of ptrarr[counter][1], use IDarr[counter].
Use MAX_ITEMS in the expression of the while statement instead of the magic number 100.
int counter = 0;
while(counter<MAX_ITEMS){
When processing "clear", you don't need to deallocate or allocate memory. Just reset counter.
else if(command=="clear")
{
counter = 0;
cout << "Entries cleared" << endl;
}
Make sure to deallocate memory before returning from main.
Here's the complete main function with the changes:
int main(int argc, char** argv) {
const int MAX_ITEMS = 100;
ID* IDarr = new ID[MAX_ITEMS];
int counter = 0;
while(counter < MAX_ITEMS){
cout << "Type add to create a new entry" << endl;
cout << "Type list to see all entries" << endl;
cout << "Type clear to delete all entries" << endl;
cout << "Type exit to terminate" << endl;
string command = "0";
getline (cin,command);
if(command=="add")
{
cout << "Enter name" << endl;
getline (cin, IDarr[counter].name);
cout << "VIP? 1 for yes, 0 for no" << endl;
cin >> IDarr[counter].vip;
cout << "Enter score" << endl;
cin >> IDarr[counter].score;
cout << "Month of birth" << endl;
cin >> IDarr[counter].date.month;
cout << "Year of birth" << endl;
cin >> IDarr[counter].date.year;
counter++;
}
else if(command=="list")
{
for (int i=0; i<counter; i++)
{
int n=i+1;
cout << n << " " << IDarr[i].name << " ";
if (IDarr[i].vip)
{
cout << "VIP ";
}
cout
<< "Score: " << IDarr[i].score << " "
<< "Born: " << IDarr[i].date.month << "/" << IDarr[i].date.year << endl;
}
}
else if(command=="clear")
{
counter = 0;
cout << "Entries cleared" << endl;
}
else if(command=="exit")
{
// Don't use return 0;
// Just break out of the while loop so that memory
// can be deallocated at the end of this function.
break;
}
else
cout << "try again" << endl;
}
delete [] IDarr;
return 0;
}
Array indices start at 0.
ptrarr[counter][1] refers to the second element of ptrarr[counter]. ptrarr[counter] points to an array of one element.
try this :
if(command=="add") {
cout << "Enter name" << endl;
getline (cin,ptrarr[counter][0].name);
cout << "VIP? 1 for yes, 0 for no" << endl;
cin >> ptrarr[counter][0].vip;
cout << "Enter score" << endl;
cin >> ptrarr[counter][0].score;
cout << "Month of birth" << endl;
cin >> ptrarr[counter][0].date.month;
cout << "Year of birth" << endl;
cin >> ptrarr[counter][0].date.year;
counter++;
}
else if(command=="list") {
for (int i=0; i<counter; i++){
int n=i+1;
cout << n << " "<< ptrarr[i][0].name << " ";
if (ptrarr[i][0].vip){
cout << "VIP ";
}
cout << "Score: " << ptrarr[i][0].score << " "
<< "Born: " << ptrarr[i][0].date.month << "/" << ptrarr[i][0].date.year << endl;
}
}
Conclusion :
Just as you initialized counter with 0 you should have used 0 index to access the first element;
Same goes while listing.
Arrays are 0 index based.

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

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