谁能帮我DEBUG一下这几段代码?~

爱睡觉的鱼

新手上路
注册
2002-12-09
消息
705
荣誉分数
2
声望点数
0
刚学C++
怎么调试都有问题。
回家了手头又没编译器,明早就要交了~
谁帮我弄好我请客吃麦当劳啊~~

谢谢咯~:thanks:

#include <iostream>

using std::cout;
using std::endl;

#include <ctime>

#include <cstdlib>

enum Months { JAN = 1, FEB, MAR, APR, MAY, JUN,
JUL, AUG, SEP, OCT, NOV, DEC };

void generateDate( int &, int &, int );
void printDate( Months, int, int );
int validDate( int, int );

int main()
{
int month = 1;
int day = 1;
int year = 1900;

srand( time( 0 ) );

printDate( month, day, year );
year = getYear( year );
getMonth();
day = getDay();

if ( validDate( month, day, year ) == true )
printDate( month, day, year );

return 0;

} // end main

// return month
int getMonth()
{
Month myMonth = rand() % 12 + 1;

return myMonth;

} // end getMonth

// return year
int getYear( int aYear )
{
return rand() % 101 + 1900;

} // end getYear

// return day
void getDay()
{
return rand() % 31 + 1;

} // end getDay

// output date
void printDate( Months month, int day, year );
{
switch ( month ) {

case JAN:
cout << "January " << day << ", " << year << endl;

case FEB:
cout << "February " << day << ", " << year << endl;
break;

case MAR:
cout << "March " << day << ", " << year << endl;
break;

case APR:
cout << "April " << day << ", " << year << endl;
break;
case MAY:
cout << "May " << day << ", " << year << endl;
break;

case JUN:
cout << "June " << day << ", " << year << endl;
break;

case JUL:
cout << "July " << day << ", " << year << endl;
break;

case AUG:
cout << "August " << day << ", " << year << endl;
break;

case SEP:
cout << "September " << day << ", " << year << endl;
break;
case OCT:
cout << "October " << day << ", " << year << endl;
break;

case NOV:
cout << "November " << day << ", " << year << endl;
break;

case DEC:
cout << "December " << day << ", " << year << endl;
break;

default:
cout << "invalid month\n";

} // end switch

} // end printDate

// check for validDate
bool validDate( int month, int day, int year )
{
int month;
int day;
int year;

if ( year < 1900 || year > 2001 )
return false;

else if ( month < 1 || month > 12 )
return false;
else if ( day < 1 || day > 31 )
return false;

else if ( day == 31 && month == APR || month == JUN
|| month == SEP || month == NOV )
return false;

else if ( month == 2 && day > 28 )
return false;

} // end validDate

#include <iostream>

using std::cout;
using std::endl;

#include <iomanip>

using std::setw;

#include <ctime>

const int NUM_GRADES = 10;
const int NUM_SUDENTS = 3;

int findHighest( int );
int findLowest( int * );
void printDatabase( const int [][], const char [][ 20 ] );

int main()
{
int student1[ NUM_GRADES ] = { 0 };
int student2[ NUM_GRADES ] = { 76, 89, 81, 42, 66, 93, 104,
91, 71, 85, 105 };
int student3[ NUM_GRADES ] = { 65, 69, 91, 89, 82, 93, 72,
76, 79, 99 };
char names[ NUM_SUDENTS ][ 20 ] = { "Bob", "John", "Joe" };

int database[ NUM_SUDENTS ][ NUM_GRADES ];
int i = 0;

srand( time( 0 ) );

// initialize student1
for ( i = 0; i < NUM_GRADES; i++ )
student1[ NUM_GRADES ] = rand() % 50 + 50;

// initialize database
for ( i = 1; i < NUM_GRADES; i++ ) {
database[ 0 ][ i ] = student1[ i ];
database[ 1 ][ i ] = student2[ i ];
database[ 2 ][ i ] = student3[ i ];

} // end for

printDatabase( database, studentNames );

for ( i = 0; i < NUM_SUDENTS; i++ ) {
cout << studentNames[ i ] << "'s highest grade is: "
<< findHighest( student1 ) << endl
<< studentNames[ i ] << "'s lowest grade is: "
<< findLowest( database[ i ] ) << endl;

} // end for


return 0;

} // end main

// determine largest grade
int findHighest( int )
{
int highest = a[ 0 ];

for ( int i = 1; i <= NUM_GRADES; i++ )
if ( a[ i ] > highest )
highest = a[ i ];

return highest;

} // end function findHighest

// determine lowest grade
int findLowest( int a[] )
{
int lowest = a[ 0 ];

for ( int i = 1; i < NUM_GRADES; i++ )
if ( a[ i ] < lowest )
lowest = a[ i ];

return lowest;

} // end lowestGrade

// output data
void printDatabase( int a[][ NUM_GRADES ], char names[][ 20 ] )
{
cout << "Here is the grade database\n\n"
<< setw( 10 ) << "Name";

for ( int n = 1; n <= NUM_GRADES; n++ )
cout << setw( 4 ) << n;

cout << endl;

for ( int i = 0; i < NUM_SUDENTS; i++ ) {
cout << setw( 10 ) << names[ i ];

for ( int j = 0; j < NUM_GRADES; j++ )
cout << setw( 4 ) << a[ i, j ];

cout << endl;

} // end for

cout << endl;

} // end printDatabase

#include <iostream>

using std::cout;
using std::cin;
using std::endl;

#include <cstring>

void initialize( char [], int * );
void input( const char [], int & );
void print( const char *, const int );
void growOlder( const char [], int * );
bool comparePeople( const char *, const int *,
const char *, const int * );

int main()
{
char name1[ 25 ];
char name2[ 25 ];
int age1;
int age2;

initialize( name1, &age1 );
initialize( name2, &age2 );

print( name1, *age1 );
print( name2, *age2 );

input( name1, age1 );
input( name2, age2 );

print( &name1, &age1Ptr );
print( &name2, &age1Ptr );

growOlder( name2, age2 );

if ( comparePeople( name1, &age1, name2, &age2 ) )
cout << "Both people have the same name and age"
<< endl;

return 0;

} // end main

// function input definition
void input( const char name[], int &age )
{
cout << "Enter a name: ";
cin >> name;

cout << "Enter an age: ";
cin >> age;
cout << endl;

} // end function input

// function initialize definition
void initialize( char name[], int *age )
{
name = "";
age = 0;

} // end function initialize

// function print definition
void print( const char name[], const int age )
{
cout << "The value stored in variable name is: "
<< name << endl
<< "The value stored in variable age is: "
<< age << endl << endl;

} // end function print

// function growOlder definition
void growOlder( const char name[], int *age )
{
cout << name << " has grown one year older\n\n";
*age++;

} // end function growOlder

// function comparePeople definition
bool comparePeople( const char *name1, const int *age1,
const char *name2, const int *age2 )
{
return ( age1 == age2 && strcmp( name1, name2 ) );

} // end function comparePeople
 
19logic errors~今天较忙~没法帮你
 
最初由 爽晔 发布
19logic errors~今天较忙~没法帮你

:(
很简单额~就是我手头没编译器~~找不出错~~
 
还是国内有D版方便,随便就去买一张了~
:smokin:
 
最初由 爱睡觉的鱼 发布


:(
很简单额~就是我手头没编译器~~找不出错~~
我现在满脑子都是digital logic的code,要转成c++比较麻烦~
刚才替你compile了第一段,说是一堆错~无奈了~
 
第一个文件
代码:
--------------------Configuration: som - Win32 Debug--------------------
Compiling...
som.cpp
c:\documents and settings\陪你去看龙卷风\desktop\som.cpp(25) : error C2664: 'printDate' : cannot convert parameter 1 from 'int' to 'enum Months'
        Conversion to enumeration type requires an explicit cast (static_cast, C-style cast or function-style cast)
c:\documents and settings\陪你去看龙卷风\desktop\som.cpp(26) : error C2065: 'getYear' : undeclared identifier
c:\documents and settings\陪你去看龙卷风\desktop\som.cpp(27) : error C2065: 'getMonth' : undeclared identifier
c:\documents and settings\陪你去看龙卷风\desktop\som.cpp(28) : error C2065: 'getDay' : undeclared identifier
c:\documents and settings\陪你去看龙卷风\desktop\som.cpp(30) : error C2660: 'validDate' : function does not take 3 parameters
c:\documents and settings\陪你去看龙卷风\desktop\som.cpp(31) : error C2664: 'printDate' : cannot convert parameter 1 from 'int' to 'enum Months'
        Conversion to enumeration type requires an explicit cast (static_cast, C-style cast or function-style cast)
c:\documents and settings\陪你去看龙卷风\desktop\som.cpp(39) : error C2373: 'getMonth' : redefinition; different type modifiers
c:\documents and settings\陪你去看龙卷风\desktop\som.cpp(40) : error C2065: 'Month' : undeclared identifier
c:\documents and settings\陪你去看龙卷风\desktop\som.cpp(40) : error C2146: syntax error : missing ';' before identifier 'myMonth'
c:\documents and settings\陪你去看龙卷风\desktop\som.cpp(40) : error C2065: 'myMonth' : undeclared identifier
c:\documents and settings\陪你去看龙卷风\desktop\som.cpp(48) : error C2373: 'getYear' : redefinition; different type modifiers
c:\documents and settings\陪你去看龙卷风\desktop\som.cpp(55) : error C2373: 'getDay' : redefinition; different type modifiers
c:\documents and settings\陪你去看龙卷风\desktop\som.cpp(56) : error C2562: 'getDay' : 'void' function returning a value
        c:\documents and settings\陪你去看龙卷风\desktop\som.cpp(54) : see declaration of 'getDay'
c:\documents and settings\陪你去看龙卷风\desktop\som.cpp(61) : error C2061: syntax error : identifier 'year'
c:\documents and settings\陪你去看龙卷风\desktop\som.cpp(62) : error C2447: missing function header (old-style formal list?)
c:\documents and settings\陪你去看龙卷风\desktop\som.cpp(120) : error C2082: redefinition of formal parameter 'month'
c:\documents and settings\陪你去看龙卷风\desktop\som.cpp(121) : error C2082: redefinition of formal parameter 'day'
c:\documents and settings\陪你去看龙卷风\desktop\som.cpp(122) : error C2082: redefinition of formal parameter 'year'
Error executing cl.exe.

som.obj - 18 error(s), 0 warning(s)
 
第二个有个错误
其他不太清楚
// determine largest grade
int findHighest( int )
{
int highest = a[ 0 ];

for ( int i = 1; i <= NUM_GRADES; i++ )
if ( a[ i ] > highest )
highest = a[ i ];

return highest;

} // end function findHighest

不能这么写
你后头用的是
cout << studentNames[ i ] << "'s highest grade is: " << findHighest( student1 ) << endl;
cout << studentNames[ i ] << "'s lowest grade is: " << findLowest( database[ i ] ) << endl;

student1是个array
 
第一个的重写 v2
代码:
#include <iostream>

using std::cout;
using std::endl;

#include <ctime>
#include <cstdlib>

enum Months { JAN = 1, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC };

void generateDate( int &, int &, int );
void printDate( Months, int, int );
bool validDate( int, int, int);
int getMonth();
int getYear( int );
int getDay();

int main()
{
	Months month = JAN;
	int day = 1;
	int year = 1900;

	srand( time( 0 ) );

	printDate( month, day, year );
	year = getYear( year );
	getMonth();
	day = getDay();

	if ( validDate( month, day, year ) == 1 )
	printDate( month, day, year );

	return 0;

} // end main

int getMonth(){ return rand() % 12 + 1; } // end getMonth
int getYear( int aYear ){ return rand() % 101 + aYear; } // end getYear
int getDay(){ return rand() % 31 + 1; } // end getDay

void printDate( Months month, int day, int year )
{
	switch ( month ) {

		case JAN: cout << "January " << day << ", " << year << endl; break;
		case FEB: cout << "February " << day << ", " << year << endl; break;
		case MAR: cout << "March " << day << ", " << year << endl; break;
		case APR: cout << "April " << day << ", " << year << endl; break;
		case MAY: cout << "May " << day << ", " << year << endl; break;
		case JUN: cout << "June " << day << ", " << year << endl; break;
		case JUL: cout << "July " << day << ", " << year << endl; break;
		case AUG: cout << "August " << day << ", " << year << endl; break;
		case SEP: cout << "September " << day << ", " << year << endl; break;
		case OCT: cout << "October " << day << ", " << year << endl; break;
		case NOV: cout << "November " << day << ", " << year << endl; break;
		case DEC: cout << "December " << day << ", " << year << endl; break;
		default: cout << "invalid month\n"; break;

	} // end switch
} // end printDate

bool validDate( int month, int day, int year )
{

	if ( year < 1900 || year > 2001 ) return false;
	else if ( month < 1 || month > 12 ) return false;
	else if ( day < 1 || day > 31 ) return false;
	else if ( day == 31 && month == APR || month == JUN || month == SEP || month == NOV )
		return false;
	else if ( month == 2 && day > 28 )
	return false;

	return false;
} // end validDate
 
卡尔顿工程系的?
还好我不上工程系,呵呵
第二个
代码:
#include <iostream>

using std::cout;
using std::endl;

#include <iomanip>

using std::setw;

#include <ctime>

const int NUM_GRADES = 10;
const int NUM_SUDENTS = 3;

int findHighest( int [] );
int findLowest( int * );
void printDatabase( const int [][NUM_GRADES], const char [][ 20 ] );

int main()
{
	int student1[ NUM_GRADES ] = { 0 };
	int student2[ NUM_GRADES ] = { 76, 89, 81, 42, 66, 93, 104, 91, 71, 85/*, 105*/ };
	int student3[ NUM_GRADES ] = { 65, 69, 91, 89, 82, 93, 72, 76, 79, 99 };
	char studentNames[ NUM_SUDENTS ][ 20 ] = { "Bob", "John", "Joe" };

	int database[ NUM_SUDENTS ][ NUM_GRADES ];
	int i = 0;

	srand( time( 0 ) );
	// initialize student1
	for ( i = 0; i < NUM_GRADES; i++ )
		student1[ NUM_GRADES ] = rand() % 50 + 50;

	// initialize database
	for ( i = 1; i < NUM_GRADES; i++ ) {
		database[ 0 ][ i ] = student1[ i ];
		database[ 1 ][ i ] = student2[ i ];
		database[ 2 ][ i ] = student3[ i ];
	} // end for

	printDatabase( database, studentNames );

	for ( i = 0; i < NUM_SUDENTS; i++ ) {
		cout << studentNames[ i ] << "'s highest grade is: "
		<< findHighest( student1 ) << endl
		<< studentNames[ i ] << "'s lowest grade is: "
		<< findLowest( database[ i ] ) << endl;
	} // end for

	return 0;
} // end main

// determine largest grade
int findHighest( int a[])
{
	int highest = a[ 0 ];
	for ( int i = 1; i <= NUM_GRADES; i++ )
		if ( a[ i ] > highest )
			highest = a[ i ];

	return highest;
} // end function findHighest

// determine lowest grade
int findLowest( int a[] )
{
	int lowest = a[ 0 ];
	for ( int i = 1; i < NUM_GRADES; i++ )
		if ( a[ i ] < lowest )
			lowest = a[ i ];

	return lowest;
} // end lowestGrade

// output data
void printDatabase( int a[][ NUM_GRADES ], char names[][ 20 ] )
{
	cout << "Here is the grade database\n\n" << setw( 10 ) << "Name";

	for ( int n = 1; n <= NUM_GRADES; n++ )
		cout << setw( 4 ) << n;
	cout << endl;

	for ( int i = 0; i < NUM_SUDENTS; i++ ) {
		cout << setw( 10 ) << names[ i ];

	for ( int j = 0; j < NUM_GRADES; j++ )
		cout << setw( 4 ) << a[ i, j ];
	cout << endl;
} // end for

//cout << endl;

} // end printDatabase
 
第三个
前2个我没测试,不过编译通过,第三个有点儿意思,我测试了,没问题,不过你没加如果用户输入大于25的话,怎么办。

代码:
#include <iostream>

using std::cout;
using std::cin;
using std::endl;

#include <cstring>

void initialize( char [], int * );
void input( char*, int & );
void print( const char *, const int );
void growOlder( const char [], int * );
bool comparePeople( const char *, const int *,const char *, const int * );

int main()
{
	char name1[ 25 ];
	char name2[ 25 ];
	int age1;
	int age2;

	initialize( name1, &age1 );
	initialize( name2, &age2 );

	print( name1, age1 );
	print( name2, age2 );

	input( name1, age1 );
	input( name2, age2 );

	print( name1, age1 );
	print( name2, age2 );

	growOlder( name2, &age2 );

	if ( comparePeople( name1, &age1, name2, &age2 ) )
	cout << "Both people have the same name and age"
	<< endl;

	return 0;

} // end main

// function input definition
void input( char *name, int &age )
{
	cout << "Enter a name: ";
	cin >> name;
	cout << "Enter an age: ";
	cin >> age;
	cout << endl;
} // end function input

// function initialize definition
void initialize( char name[], int *age )
{
	name = "";
	age = 0;
} // end function initialize

// function print definition
void print( const char name[], const int age )
{
	cout << "The value stored in variable name is: "
	<< name << endl
	<< "The value stored in variable age is: "
	<< age << endl << endl;
} // end function print

// function growOlder definition
void growOlder( const char name[], int *age )
{
	cout << name << " has grown one year older\n\n";
	*age++;
} // end function growOlder

// function comparePeople definition
bool comparePeople( const char *name1, const int *age1, const char *name2, const int *age2 )
{
	return ( age1 == age2 && strcmp( name1, name2 ) );
} // end function comparePeople
 
自己最近闲的发慌

要VC++的话,可以到学校MSDNAA下载正版的
所有这些系的学生都可以免费下载
http://msdnaa.carleton.ca/
* MSDNAA - Department of Electronics (DOE)
* MSDNAA - Mechanical and Aerospace Engineering (MAE)
* MSDNAA - School of Computer Science (SCS)
* MSDNAA - School of Information Technology (BIT)
* MSDNAA - School of Mathematics and Statistics
* MSDNAA - Sprott School of Business
* MSDNAA - Systems and Computer Engineering (SCE)
 
我当年学JAVA的时候 怎么没遇到这样的好人啊。。。
 
后退
顶部