[c++] can someone double check me

Jan 29, 2009 at 12:38am
basically, this is what i have to do:

In case 1 for the switch
(b) print a blank line, a line with the message:"Part I:" and a blank line.
© Prompt the user to enter a text message from the keyboard
and accept a text from the keyboard by using gets().
(d) Print the original message on the screen.
(e) Find the number of characters in the message by using strlen() and
print the message in the reverse order. For example, "ROSIE IS CUTE!"
becomes "!ETUC SI EISOR".

In case 2 for the switch
(f) print a blank line, a line with the message:"Part II:" and a blank line.
(g) Print a message:
"Series evaluation: x^n/n! from n=0 to n=n_end.\n" and a blank line.
In the above n! (n factorial)=1*2*...*n for n>0 and 0!=1.
(h) Enter x, a real number and n_end, a large positive integer
from the keyboard and print those values using %g and %d.
(i) In a for-loop evaluate the sum. In addition, print the partial sums
using %16.8e for n=10, 20, 30,.... Print the values of n_end, sum,
and x using %4d, %16.8e, and %g. Finally, print the value of
exp(x) using %g for x and %16.8e for exp(x).


I tried to do part one and i think i got most of it but it doesnt work when i do the a.out. any suggestions?

#include <stdio.h>
#include <math.h>
int main(void)
{
int menu, n, n_end, length;
char message[111];
printf("\nHomework 4:\n\n");
printf("There are two parts: part 1 and part 2.\n");
printf("Enter the part number to execute (1 or 2):");
scanf("%d", &menu);

switch(menu){
case 1:
printf("Part I:\n\n");
printf("\nEnter a text message:");

gets(text);
printf("echo: %s\n", text);
length = (int)strlen(text);
printf("There are %d characters.\n", length);
for(k=length-1; k>=0; k--) printf("%c", text[k]);
printf("\n");
printf("\nPart II:\n\n");

}
Last edited on Jan 29, 2009 at 1:33am
Jan 29, 2009 at 1:42am
OMG. Unless this is a C assignment and not C++ I'd be having a word to your tutor.

1. You shouldn't used a fix length character array when you can use a string instead.
2. printf() should be replaced by cout <<
3. scanf should be replaced by getline()
4. gets() should never be used, it has no bounds checking

All of these could be done using built in C++ types and IOStreams. This assignment is teaching nothing but bad practices.

Apart from that, your code matches the requirements. It's the requirements that are not of good practice. The only thing I'd do is have char message[1024] = {0};
Last edited on Jan 29, 2009 at 1:44am
Jan 29, 2009 at 4:21am
this could be C. i dont know. my class title is intro to c/c++ so could be either. he doesnt tell us which ones it is... he just gives us the assignment and i guess assumes that we know, but he's a pretty bad teacher so i dont jack about c/c++ which is why im here.. anyone else help out?
Jan 29, 2009 at 4:32am
Mmh... Yep. That's C, alright.
And yes, your code is correct. How doesn't it work? What happens when you run it?
Jan 30, 2009 at 8:45am
ok, this is what it says after i compile.
#include <stdio.h>
#include <math.h>
int main(void)
{
int menu, n, n_end, length;
char message[111];
printf("\nHomework 4:\n\n");
printf("There are two parts: part 1 and part 2.\n");
printf("Enter the part number to execute (1 or 2):");
scanf("%d", &menu);

switch(menu){
case 1:
printf("Part I:\n\n");
printf("\nEnter a text message:");

gets(text);
printf("echo: %s\n", text);
length = (int)strlen(text);
printf("There are %d characters.\n", length);
for(k=length-1; k>=0; k--) printf("%c", text[k]);
printf("\n");
printf("\nPart II:\n\n");
}
}


"hw4.c" 26 lines, 691 characters
iacs5.ucsd.edu% gcc hw4.c
hw4.c: In function `main':
hw4.c:17: error: `text' undeclared (first use in this function)
hw4.c:17: error: (Each undeclared identifier is reported only once
hw4.c:17: error: for each function it appears in.)
hw4.c:21: error: `k' undeclared (first use in this function)


can someone please help me? and can someone help me out with part 2 also? dont really understand what im supposed to do. thanks.
Jan 30, 2009 at 1:58pm
yep, your code is all right, but you just missed to declare 'text' and 'k', that's why, when the copiler run, kick out an error, cause it dont know what 'text' and 'k' are.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <stdio.h>
#include <math.h>
 int main(void)
{
  int menu, n, n_end, length;
  char message[111]; /* char text[111];  or some number declare 'text' here. */
  printf("\nHomework 4:\n\n");
  printf("There are two parts: part 1 and part 2.\n");
  printf("Enter the part number to execute (1 or 2):");
  scanf("%d", &menu);

  switch(menu)
   {
      case 1:
         printf("Part I:\n\n");
         printf("\nEnter a text message:");

         gets(text);       /* here you are using 'text', but you havent declared it yet. */
         printf("echo: %s\n", text);
         length = (int)strlen(text);
         printf("There are %d characters.\n", length);
        for(k=length-1; k>=0; k--) printf("%c", text[k]);   /* k is not declared yet, so its goes like: for( int k=length-1; k>=0; k--) */
         printf("\n");
         printf("\nPart II:\n\n");
    }
}

Last edited on Jan 30, 2009 at 2:10pm
Jan 31, 2009 at 12:31am
ok thanks. i fixed it and got the first part down. can someone just help me do this:

Print a message:
"Series evaluation: x^n/n! from n=0 to n=n_end.\n" and a blank line.
In the above n! (n factorial)=1*2*...*n for n>0 and 0!=1.

Enter x, a real number and n_end, a large positive integer
from the keyboard and print those values using %g and %d.

In a for-loop evaluate the sum. In addition, print the partial sums
using %16.8e for n=10, 20, 30,.... Print the values of n_end, sum,
and x using %4d, %16.8e, and %g. Finally, print the value of
exp(x) using %g for x and %16.8e for exp(x).

after the printf(""Series evaluation: x^n/n! from n=0 to n=n_end.\n"); how do u the other parts?
Feb 9, 2009 at 3:46pm
Please Use standard position

in order to be clean

and your codes is not

confusing.....

ok standard form.
Feb 9, 2009 at 7:26pm
@raprap321: Your answer makes no sense. Please use proper English when replying to other peoples posts. Yours is more confusing than the OP.
Topic archived. No new replies allowed.