Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++
= = =>> W E I R D - E R R O R <<= = =
 
// 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 8-Apr-13 11:39am
ctype.h1.7K
Edited 9-Apr-13 0:46am
v3
Comments
Sergey Alexandrovich Kryukov at 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 at 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 at 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
tech.nerd at 8-Apr-13 16:54pm
   
ok .. updated .. nOw .. Same Problem .. :(
Sergey Alexandrovich Kryukov at 8-Apr-13 17:12pm
   
No, not update what I meant. It was just for start, to make the sound code, not to solve the problem, not just yet.
Did you use the debugger?
—SA
Matthew Faithfull at 8-Apr-13 17:09pm
   
Is this failure data dependent? Try entering the sequence a b c d e ... for the names of the students and see if this still occurs.
lewax00 at 9-Apr-13 0:57am
   
What are you entering for names? Keep in mind, cin.getline null terminates strings, so you actually only have room for 9 characters.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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:
 
struct student
{
    char name[5][10];
    int id[5];
};
 
needs to be:
 
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:
 
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:
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;
  Permalink  
v2
Comments
tech.nerd at 9-Apr-13 5:43am
   
Wow Thanks for the justification .. I Give 5 :)
tech.nerd at 9-Apr-13 5:44am
   
Question Updated But There Is The Birth Of New Error, I ever See ..
PLEASE HELP .. !!!!
The_Inventor at 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.
tech.nerd at 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 at 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.
tech.nerd at 11-Apr-13 5:34am
   
Thank You Sir ..
I'm Happy now .. :)
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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:
struct Student {
   char name[5][11];
   int id[5];
}
  Permalink  
Comments
tech.nerd at 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 at 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.
tech.nerd at 11-Apr-13 5:33am
   
Thank You Sir .. 5
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Try:
 
for (int i = 0; i <= size; i++)
{
...;
}
  Permalink  
Comments
lewax00 at 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)

  Print Answers RSS
0 George Jonsson 215
1 Kornfeld Eliyahu Peter 169
2 OriginalGriff 120
3 PIEBALDconsult 110
4 BillWoodruff 85
0 OriginalGriff 6,165
1 DamithSL 4,658
2 Maciej Los 4,087
3 Kornfeld Eliyahu Peter 3,649
4 Sergey Alexandrovich Kryukov 3,294


Advertise | Privacy | Mobile
Web01 | 2.8.141220.1 | Last Updated 11 Apr 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100