Click here to Skip to main content
15,882,017 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
= = =>> W E I R D - E R R O R <<= = =

C++
// error.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <string>

using namespace std;

struct Student
{
	const char name[6][11];
	const int id[5];
};


void fetch_id(Student& s, const int size)
{
	for (int i = 0; i < size; i++)
	{
		cout << "roll no of student: " << i+1 << endl;;
		cin >> s.id[i];
	}
	
}
void fetch_name(Student& s, const int size)
{
	
		for (int j = 0; j < size; j++)
		{
			
			cin.getline(s.name[j], 10);
			cout <<"name of student: " << j+1 << endl;	
		}
	
}

void display_name(Student s, const int size)
{
	cout << "Student Names Are" << endl;
	for (int i = 0; i < size; i++)
	{			
		if ( s.name[i] != '\0' )
		cout << s.name[i] << endl;
	}
}

void display_id(Student s, const int size)
{
	cout << "Roll Numbers Are" << endl;
	for (int i = 0; i < size; i++)
	{
		cout << s.id[i] << " || ";
	}
}

int main()
{
	Student s; //  error C2512: 'Student' : no appropriate default constructor available ??
	const int size = 5;
	fetch_id(s, size);
	display_id(s, size);
	cout << '\n';
	fetch_name(s, size);
	cout << '\n';
	display_name(s, size);
	system("Pause");
	return 0;
}
Posted
Updated 8-Apr-13 23:46pm
v3
Comments
Sergey Alexandrovich Kryukov 8-Apr-13 16:43pm    
This code it not good enough to discuss seriously. To start with, eliminate hard-coding of immediate constants like 5 and 10, declare constants explicitly as const or even make those number parameters of some funцtion...
—SA
Stefan_Lang 9-Apr-13 11:03am    
At first I couldn't make sense of your comment, but knowing the quality of your contributions I had a closer look: it seems the question has been drastically altered to the point where your comment doesn't apply any more...

That said, after looking at version 1 of the question, I totally agree with you.
Sergey Alexandrovich Kryukov 9-Apr-13 11:19am    
OK, thank you very much for the note. I'm very glad that OP listened to the criticism and improved at least some of the code. This is a very good sign.
I really wish OP best of luck and success.
—SA
Usman Hunjra 8-Apr-13 16:54pm    
ok .. updated .. nOw .. Same Problem .. :(

Well this is where the issue lies, with the indexing, the actual size of the array, and actual available spots for storage.

Using -> char name[5][10]; gives him only four place to store info and each name gets only nine char's. Zero based indexes are different that one based. If he had used CArray, then his 'for()' statement would work. So in this case:

C#
struct student
{
    char name[5][10];
    int id[5];
};


needs to be:

C#
struct student
{
    char name[6][11];
    int id[6];
};



to get a total of five students, each with up to ten characters to their name.

The other issue is his:

C++
for (int j = 0; j < size; j++)
		{
			cout <<"name of student: " << j+1 << endl;	
			cin.getline(s.name[j], 10);
		}

Here (above) he has reversed the logical order of the statements within the {}.
It might be better as:
C++
for (int j = 0; j < size; j++)
		{
			cin.getline(s.name[j], 10);
			cout <<"name of student: " << j+1 << endl;	
		}


as he has incremented his index within the body of the for statement.
In C++ the 'for loop' will increment its index before checking the middle statement j < size;
 
Share this answer
 
v2
Comments
Usman Hunjra 9-Apr-13 5:43am    
Wow Thanks for the justification .. I Give 5 :)
Usman Hunjra 9-Apr-13 5:44am    
Question Updated But There Is The Birth Of New Error, I ever See ..
PLEASE HELP .. !!!!
The_Inventor 10-Apr-13 2:57am    
Look again very carefully at what I did (see the needs to be section) as you didn't fully implement what I did.
Usman Hunjra 9-Apr-13 5:53am    
Sir .. I'm Little Puzzled here ..
why there is only 4 places to input char name[5][10].. Null character is going to embed on length side.. Why this stores 4 names Only .. :/ ?? :(
The_Inventor 10-Apr-13 3:23am    
It is the difference between 'index' and 'size'. When you use [] the number in between the [] is the "upperbound" while size = upperbound-1, with size the actual number of 'elements' that the array can hold. Look for a file on your computer called afxtempl.h, it has the code that developes the C++ class CArray. Using [] is the old method of declaring an array.
There are several problems at hand:

First, I've read your question (version 3) and the already existing solutions and comments. But then I ended up being totally confused, because part of the discussion didn't appear to make sense. The problem of course was that you drastically altered the text of your question, deleting the original information and question in the process!

Please do not do such a thing - it is very confusing and makes it a lot harder to provide actual help! Not everyone will realise you changed your question, or even knows how to look at previous versions (do you?). Also, changing your question will not - I repeat: not - in any way notify the people who already tried to help you about this change! If you're lucky, they may take another look to see if you need any more help, but even then there is no guarantee they even realize that you changed something in your posting!

Instead, if you tried something and that raised another question, post that information as a response to the comment or solution that prompted you to perform this change. Doing so will normally notify the person you responded to, and he will thus be able to react to your change and new problem.

Second your understanding of basic C and C++ types appears to be lacking: for one, in C++ you should use std::string for the purpose of storing strings. Using char arrays was the norm in C, but you need to understand that in C the end of a string is expected to be terminated by a 0-character. Any function working on strings therefore will either try to locate that terminating 0 (when reading), or append a 0 to the end (when writing). If you were using std::string, you wouldn't need to take care of that, and also could use some advanced string functions.

Third, you can not assign a value to a variable declared as const (I'm referring to version 3 - there was no const in version 1). For const values declared inside a class or struct, it's a bit more complex: you can, and indeed must, assign a value. And the only place to do that is during construction. in other words, you must have a constructor that assigns the values of the members that are const.

This is also the cause for that constructor error message: the compiler does generate a default constructor for you, but that one will not assign any values to your members. So you're lacking a constructor that does the proper const initialization.

Of course, it was an error to declare your member variables as const in the first place, as you wish to modify them. The following should work:
C++
struct Student {
   char name[5][11];
   int id[5];
}
 
Share this answer
 
Comments
Usman Hunjra 11-Apr-13 3:36am    
Yes Sir .. Thank you for your consideration ..
Sir please check my solution..
The problem is in the loop handling condition..
Stefan_Lang 11-Apr-13 4:51am    
I did. What I wrote as a third point explains the cause of the compiler error. Looking at version 3 of your posting, the loops are ok, except for the logic in fetch_name(). but that has already been explained in solution 2, so I didn't mention it.

There are various other things that could be improved, but none that prevent your program from working.
Usman Hunjra 11-Apr-13 5:33am    
Thank You Sir .. 5
Try:

for (int i = 0; i <= size; i++)
{
...;
}
 
Share this answer
 
Comments
lewax00 9-Apr-13 0:55am    
No, that would index the array to size (5 in this case), which would be beyond the bounds of the array. That would be unsafe code, as you have no idea what's being stored there.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900