-
String Array in C++
I'm trying to create a string array in C++
Code:
const int pocetJmena = 8;
string[] jmena = new string[pocetJmena];
jmena[0] = "Ruzicka Vit";
jmena[1] = "Bily Saruman";
jmena[2] = "Veliky Saruman";
jmena[3] = "Calculator Veliky";
jmena[4] = "Jakekoliv Jmeno";
jmena[5] = "Nekdo Veliky";
jmena[6] = "Ahoj Vitek";
jmena[7] = "Whatever Name";
I'm including the definitions
Code:
#include <string>
using std::string;
but the program just won't work
"main.cpp": E2108 Improper use of typedef 'string' in function main() at line 23
"main.cpp": E2188 Expression syntax in function main() at line 23
"main.cpp": E2451 Undefined symbol 'jmena' in function main() at line 24
these are the three errors that I get. I'd be thankful for any help.
-
First of all. are you sure that your source files have a .cpp extension?
Also, you have two syntactic errors:
Code:
string[] jmena
= new string[pocetJmena];
You need to declare an array like this:
Code:
string jmena[pocetJmena]; //note: the [] appears after the variable
//populate array:
jmena[0] = "Ruzicka Vit";
jmena[1] = "Bily Saruman";
jmena[2] = "Veliky Saruman";
jmena[3] = "Calculator Veliky";
jmena[4] = "Jakekoliv Jmeno";
jmena[5] = "Nekdo Veliky";
jmena[6] = "Ahoj Vitek";
jmena[7] = "Whatever Name";
There's no need to allocate the strings dynamically -- this must be a Java habit that you should get rid of in C++....
Danny Kalev
-
c++
can somebody help me:(
i don't know what's wrong with my program:
this is my header file:
// StudentInformation.h file
#include <string> // uses string class
using std::string;
// StudentInformation class definition
class StudentInformation
{
public:
const static int students = 30; // constant number of student in the class
studentInformation ( string, string, double ); // constructor that initialize the firstname, lastname and Finalscore.
void setFirstName ( string ); // function that set the first name
string getFirstName (); // function that get the first name
void setLastName ( string ); // function that set the last name
string getLastName (); // function that get the last name
void setFinalScore ( double ); // function that set the final score
double getFinalScore (); // function that get the final score
void print (); // function that print the first name, last name and the final score
void printGrades (); // function that print all the students first name, last name and the final grade
double lowestScore (); // function that determine the lowest score
double highestScore (); // function that determine the highest score
double avgScore (); // function that determine the average score
void add ( string, string, double );
private:
string firstName; // firstname for this student information class
string lastName; // lastname for this student information class
double finalScore; // final score for this student information class
string fName[ students ]; // array of students' first name for the student information class
string lName[ students ]; // array of students' last name for the student information class
double fScore[ students ]; // array of student final score for the student information class
}; // end class student information
then this is my .cpp file:
// StudentInformation II.cpp
// Define class studentInformation that contains firstName, lastName, finalScore data
// and member functions to set and get its value.
#include <iostream> // allows program to perform input and output data
// to the screen
using namespace std;
#include <string> // program uses C++ standard string class
using namespace std;
#include <iomanip>
using namespace std;
#include "StudentInformation II.h"
// StudentInformation constructor initializes header, firstname and
// lastname with string supplied as argument and the array finalScore.
StudentInformation::StudentInformation( string f, string l, double s )
{
setFirstName( f ); // initialize firstName
setLastName( l ); // initialize lastName
setFinalScore(s);// initialize finalScore
} // end of constructor
void add( string f, string l, double s )
{
static double max = 0; // initialize first time add is called
// if max is less than students, copy firstName, lastName and finalScore into their respective arrays.
if ( max < students )
{
string fName[ max ] = firstName; // store firstName in array firstName
string lName[ max ] = lastName; // store lastName in array lastName
string fScore[ max ] = finalScore; // store finalScore in array finalScore
max++; // increment max by 1
}
} // end of constructor
// function that gets the first name
string StudentInformation::getFirstName()
{
return firstName; // return object's firstName
} // end function getFirstName
// function that sets the first name
void StudentInformation::setFirstName( string f )
{
firstName = f; // store the first name in the object
} // end function setFirstName
// function that gets the last name
string StudentInformation::getLastName()
{
return lastName; // return object's lastName
} // end function getLastName
// function that sets the last name
void StudentInformation::setLastName( string l )
{
lastName = l; // store the last name in the object
} // end function setLastName
// function that gets the finalScore
double StudentInformation::getFinalScore()
{
return finalScore; // return object's finalScore
} // end function getFinalScore
//function that sets the finalScore
void StudentInformation::setFinalScore( double s )
{
finalScore = s; // store the final score in the object
} // end function setFinalScore
// find minimum score
double StudentInformation::lowestScore()
{
double lowScore = 100; // assume lowest grade is 100
// loop through finalScore array
for ( int student = 0; student < max; student++ )
{
// if current score lower than lowScore, assign it to lowScore
if ( fScore[ student ] < lowScore )
lowScore = fScore[ student ]; // new lowest score
} // end the for loop
return lowScore; // return lowest score
} // end function lowestScore
// find maximum score
double StudentInformation::highestScore()
{
double highScore = 0; // assume highest score is 0
// loop through finalScore array
for ( int student = 0; student < max; student++ )
{
// if current score higher than highScore, assign it to highGrade
if ( fScore[ student ] > highScore )
highScore = fScore[ student ];
} // end for
return highScore; // return highest grade
} // end function highestScore
// find average score
double StudentInformation::avgScore()
{
double total = 0; // initialize total
// sum scores in array
for ( int student = 0; student < max; student++ )
total += fScore[ student ];
// return average of scores
return total / max;
} // end function average score
// function that print first name, last name and the final grade of all the students
void StudentInformation::printGrades()
{
// output each student grade
for ( int student = 0; student < max; student++ )
{
if ( fScore[ student ] >= 90 ) // if final score is more than 90, print Final Grade is A
cout<< "\n" << fName[ student ] << setw(10) << lName[ student ] << setw(10) << "Final Grade is A";
else if ( fScore[ student ] >= 80 ) // if final score is more than 80, print Final Grade is B
cout<< "\n" << fName[ student ] << setw(10) << lName[ student ] << setw(10) << "Final Grade is B";
else if ( fScore[ student ] >= 70 ) // if final score is more than 70, print Final Grade is C
cout<< "\n" << fName[ student ] << setw(10) << lName[ student ] << setw(10) << "Final Grade is C";
else if ( fScore[ student ] < 70 ) // if final score is less than 70, print Final Grade is F
cout<< "\n" << fName[ student ] << setw(10) << lName[ student ] << setw(10) << "Final Grade is F";
} // end for loop
} // end function that print the grades of all the students
// function that print first name, last name and finalScore
void StudentInformation::print()
{
cout<< "\n\nFirst Name" << setw(10) << "Last Name" << setw(10) << "Final Score\n\n";
// output each student's score
for ( int student = 0; student < max; student++ )
cout<< fName[ student ] << setw(10) << lName[ student ] << setw(10) << fScore[ student ] <<"\n";
} // end function print
now, this is my main() file:
// function main of StudentInformation II.cpp
#include <iostream>
using namespace std;
#include "StudentInformation II.h" // Include definition of class StudentInformation II
// function main begins program execution
int main()
{
// create one studentInformation object
StudentInformation myStudentInformation1( "Emeric", "Bechi", 95);
myStudentInformation1.print();
StudentInformation myStudentInformation2( "Isaac", "Newton", 100 );
myStudentInformation2.print();
system ("pause");
} // end main
now, this is the errors that i get:
5 C:\Dev-Cpp\StudentInformationIV.cpp In file included from C:\Dev-Cpp\StudentInformationIV.cpp
11 C:\Dev-Cpp\StudentInformation II.h ISO C++ forbids declaration of `studentInformation' with no type
C:\Dev-Cpp\StudentInformationIV.cpp In function `int main()':
11 C:\Dev-Cpp\StudentInformationIV.cpp no matching function for call to `StudentInformation::StudentInformation(const char[7], const char[6], int)'
-
***..
How does this post get 8700 odd views in such a short time??
-
There's something I didn't notice earlier:
string[] jmena = new string[pocetJmena];
You can't do that in C++. The left side of the expression must be a pointer. string[] is not a pointer. Furthermore, arrays in C++ must be declared like this:
string jmena[];
(you're probably used to Java but in C++ the [] must appear only after the variable's name).
Danny Kalev
-
 Originally Posted by Code_Nerd
***..
How does this post get 8700 odd views in such a short time??
Depends on what you mean by "short time". The original post in from 2005;)
Danny Kalev
-
 Originally Posted by Danny
Depends on what you mean by "short time". The original post in from 2005;)
Good point...
Did "W T F" get moderated?
-
 Originally Posted by Code_Nerd
Good point...
Did "W T F" get moderated?
Where is it? I didn't delete it, but I suppose vBulletin might have.
I prefer to spend my time answering questions, not chasing this or that acronym.
Danny Kalev
-
String Array in C++
--------------------------------------------------------------------------------
I'm trying to create a string array in C++
Code:
const int pocetJmena = 8;
string[] jmena = new string[pocetJmena];
jmena[0] = "Ruzicka Vit";
jmena[1] = "Bily Saruman";
jmena[2] = "Veliky Saruman";
jmena[3] = "Calculator Veliky";
jmena[4] = "Jakekoliv Jmeno";
jmena[5] = "Nekdo Veliky";
jmena[6] = "Ahoj Vitek";
jmena[7] = "Whatever Name";
this is java syntax, hence it will not work in c++.
-
 Originally Posted by Danny
First of all. are you sure that your source files have a .cpp extension?
Also, you have two syntactic errors:
Code:
string[] jmena
= new string[pocetJmena];
You need to declare an array like this:
Code:
string jmena[pocetJmena]; //note: the [] appears after the variable
//populate array:
jmena[0] = "Ruzicka Vit";
jmena[1] = "Bily Saruman";
jmena[2] = "Veliky Saruman";
jmena[3] = "Calculator Veliky";
jmena[4] = "Jakekoliv Jmeno";
jmena[5] = "Nekdo Veliky";
jmena[6] = "Ahoj Vitek";
jmena[7] = "Whatever Name";
There's no need to allocate the strings dynamically -- this must be a Java habit that you should get rid of in C++....
Is there anybody that can help me? I am trying to declare a static string array. But have no idea how and where it should be declared. I am very very confused. Please help??
-
#include <string>
std::string strarray[3] = {"first", "second", "third"};
Danny Kalev
-
So Confused!
Would I declare that in the header file or the cpp file?
I need to store the values in an array so that they can be used later to change the state of a lot in an auction, e.g. assigned, unassigned, or sold.
Am I going about the whole thing incorrectly!!! :(
-
Definitions should always appear in the .cpp file, especially when they contain initializers. If you add this array definition to a header file, you will get a linker error.
Danny Kalev
-
Thank you
Thank you, that explains everything. ;)
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks