Journal Articles
Browse in : |
All
> Journals
> CVu
> 113
(22)
All > Topics > Programming (877) All > Journal Columns > Code Critique (70) Any of these categories - All of these categories |
Note: when you create a new publication type, the articles module will automatically use the templates user-display-[publicationtype].xt and user-summary-[publicationtype].xt. If those templates do not exist when you try to preview or display a new article, you'll get this warning :-) Please place your own templates in themes/yourtheme/modules/articles . The templates will get the extension .xt there.
Title: Questions & Answers
Author: Administrator
Date: 03 April 1999 13:15:30 +01:00 or Sat, 03 April 1999 13:15:30 +01:00
Summary:
Body:
Forgive me, I took the liberty to take your address from the message boards on C++.
OK I will have a look this time, but please consider becoming a student member of ACCU (see www.accu.org) where we have a reflector dedicated to helping people with their code.
If you have a few minutes, please help me figure out why this C++ program compiles, runs, but does not properly execute the convert from upper to lower case function? This assignment is due Thursday(01/21/99) :-(
Programming problem: Write a program that requests a user to enter a string. Convert the string to lower case using a function you create. Your function should be stored in an external library file which you include in your C++ program using the #include directive. Beginning of main program:
#include <iostream.h>
this is classic C++ standard C++ uses #include <iostream>
<snip> #include <stdio.h>
why this as well as iostream.h, you should generally choose only one. It is not wrong to use features from both but it usually shows a lack of thought.
#include "convertc.h" const dimensions = 30;
good, nice to see you avoid the style error of writing in all upper case however you need to learn to avoid implicit int because C++ (and the next version of C) have removed it. In other words you must write int const (or if you insist, const int) instead of your plain const.
typedef char string[dimensions];
both the above should be in convertc.h (or another header so that the same rules can be applied throughout your program -- by placing them in a header file you only have a single change when you decide to do so.
string is a very bad choice of name -- string has a specific meaning in C++ and all global identifiers starting with str followed by a lower case letter are reserved by both C and C++ for use by compiler implementors.
void convert_case();
that prototype belongs in your header file
void main()
In both C and C++ main is required to return int (despite all the books and compilers telling you otherwise)
{ string your_name; cout << "Please enter your name: "; cin >> your_name; void convert_case ();
THIS IS THE CRITICAL ERROR
leave out that void (it makes this a declaration of a version of convert_case() that takes no arguments rather than the call you intended)
cout << " Hello and a nice day to you " << your_name;
to keep silly compilers happy include return 0; the language does not need it but MS compilers are too stupid to know that.
} ****end of main program**** ****begin of library file**** #ifdef convertc.h
Do not spell preprocessor names with lower case letters and a dot is not an allowed symbol in a preprocessor identifier. In addition if this is intended to be a file sentinel it is wrong. It should be #ifndef CONVERTC_H
const dimensions = 30;
See above re omitted int.
typedef char string[dimensions]; // this is the function to convert the string to lower case void convert_case(string str1);
Your header file should end here and the definition belongs in its own source code file with appropriate includes (one of which will be for convertc.h)
void convert_case(string str1) { int limit = strlen(str1); for (int x = 0; x < limit; ++x) str1[x] = _tolower(str1[x]);
why are you calling an implementors function (with leading underscore) instead of the standard function tolower()?
} #define convertc.h
Conventionally this line (corrected) is placed immediately after the #ifndef line.
#endif >****end of library file****
I hope that helps.
(Of course being a typical student he never replied ☺
Notes:
More fields may be available via dynamicdata ..