C++ Basice Help!!

Thread Starter

Leaner

Joined Mar 17, 2008
4
Dear everyone!
i doing little study on C++ an got stuck in this assignment and hoping for everyone to contribute and help.


GOAL In this program you will first create a string that has the
exact content "Hello Apple." The command line should have
either no parameters ( print id line as in the code below)
or just one parameter which is an integer.
The following text lists errors you must detect and a
priority of testing. All outputs are a single character
followed by a linefeed ( endl in C++ or \n in C).

If there is more than one parameter the output
shall be 'P'.
If the parameter is not an integer the output
shall 'X'.
The input integer is an index into the string where [0]='H',
[1]='e' etc. Your program should return the letter
pointed to by the number then a linefeed. If the number
is not within the string the reply must be Z.



*/

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <iostream>
#include <cstdlib>

#include <time.h>
#include <sys/time.h>

using namespace std;

int main(int argc, char *argv[])
{//--- When no parameters MUST print id string in CSV format.
if (argc == 1) // no parameters.
{ cout << "Hello Everyone"
<< endl ;
return(0) ;
}
//--- START THE CODE FROM HERE.
{
cout << " and the answer is ..." << endl ;

return(0) ;
}

Hope everyone can help!

Thank in advance
 

Mark44

Joined Nov 26, 2007
628
1) If there are no command-line params (i.e., argc == 1), are you supposed to display "Hello Apple" or "Hello Everyone"? The problem statement shows the first, but your code shows the second.
2) If there is one command-line param (argc == 2), you can access them from the array of pointers in *argv[].
argv[0] contains the 1st string in the command line, which is the name of your program file (including path, if memory servers).
argv[1] contains the 2nd string in the command line.
argv[2] contains the 3rd string in the command line, if present.
... and so on.
Note that when I say that argv[n] contains the (n+1)st string, what I really mean is that it contains the address of that string. If you dereference one of these array elements, you get the character stored at the beginning of the string.
To display the 2nd string in the command line (remember that the first string is the name of the program's exe file), just do this:
cout << argv[1];
Mark
 

Thread Starter

Leaner

Joined Mar 17, 2008
4
thank alot mark i get what u mean, but the "Hello everyone" is just state from the first paprams, The hello apple suppose to start after the section "start the code".

i still don understand how to display "p" if there more then 1 param. or "X" if there the param not an interger output.

the most confusion part will had to be the part how to display "Z" refer from above. it will help alot if you can explain that part for me.

thank in advance.
 

Mark44

Joined Nov 26, 2007
628
Let's look at how the program is supposed to behave, and assume for a minute that we have myprog.exe that does this. I'm going to focus on the behavior as described in the instructions and ignore introductory strings that the program should produce.

If I start the program from the command line like this:
c>myprog.exe 1 2 3
the program should display P on the screen
(BTW, you can omit .exe when you start the program)

If I start the program this way:
c>myprog cat
the program should display X on the screen

If I start it this way:
c>myprog 6
the program should display A (the character at position 6 in the string "Hello Apple"

If I start it this way:
c>myprog 23
the program should display Z (position 23 is past the end of the "Hello Apple" string)

OK, how do we get there now. Without writing the program for you, here are the basic ideas:
Check the value of argc. If it's 1, there are no command line parameters (i.e., additional information after the name of the program). If it's 3 or larger, there are too many command line parameters, so print the character P. It should be obvious that you want argc to be equal to 2.

If argc is 2, you can get the first parameter with this expression: argv[1]. Now this expression is a string and may consist of more than one character. You can convert it to an int by calling the standard library function atoi() (be sure to #include <stdlib.h>), which takes a string argument and returns an int. If atoi returns 0, it wasn't successful in the conversion, otherwise it returns the numeric value of the string that contains decimal digit characters.

To display the character at position 6, for example, and assuming that you have a string defined like so: char str[] = "Hello Apple";

all you need to do is to display str[6] using cout or printf.

If, after you convert the input string to an int, and you find out that it is larger than 10, display Z. The reason is that 10 is the index of the final 'e' in the "Hello Apple" string.

See if you can put all these ideas together to create your program. If you get stuck, let us know.
Mark
 

Thread Starter

Leaner

Joined Mar 17, 2008
4
Okay that understand able, i using linux so this will work with Kdevolope as well right.

anyway thank alot Mark.
 

Mark44

Joined Nov 26, 2007
628
I don't know what Kdevelope is, but what I've told you should work on any operating system that your C/C++ development environment runs on.

By the way, which way do you lean? Your alias is Leaner, so I figure that means you don't stand up straight.

Or did you mean to use Learner?
 

Thread Starter

Leaner

Joined Mar 17, 2008
4
haha lol sorry didn't get to you sooner. was enjoying easter. ya i mean to use Learner but though try to do some thing a bit difference. didn't expect you to be so sharp:p. anyway i try to get on the program now if there more problem i will be sure to let you know. had a good easter everyone.

thank alot.
 

Mark44

Joined Nov 26, 2007
628
didn't expect you to be so sharp:p
Leaner,
I think you'll find that people who spend a lot of time writing code are pretty aware of fine details. To be able to find and fix bugs, you need to look at things like a compiler does.
Mark
 
Top