Welcome, Guest. Please login or register.

Author Topic: I need help with C(++)  (Read 4769 times)

Description:

0 Members and 1 Guest are viewing this topic.

Offline z5Topic starter

  • Sr. Member
  • ****
  • Join Date: May 2002
  • Posts: 366
    • Show only replies by z5
    • http://ada.untergrund.net
I need help with C(++)
« on: April 03, 2003, 05:20:02 PM »
Hi!

i'm doing my exercises for my exam c(++). The compiler i use is Borland c++. I've run into this problem so i thought: why not ask on Amiga org ;-)

Here's the little program:
#include
#include
#include

void main(void)
  {
     const numberofstrings = 5;
     char str[numberofstrings][80],*p;
       
     for(p=str;p                {
                printf("Type string: ");
                gets(p);
                }
}

The problem is that my compiler doesn't allow p=str because str is a two dimensional array. I just wanted the pointer to point at the first string in my string array.

What i was trying to achieve is that the user gives a couple of strings and i store them in a string array (using pointers).

Thanks for any help
A.miga D.emoscene A.rchive: Relive the dreams...
 

Offline Kronos

  • Resident blue troll
  • Hero Member
  • *****
  • Join Date: Feb 2002
  • Posts: 4017
    • Show only replies by Kronos
    • http://www.SteamDraw.de
Re: I need help with C(++)
« Reply #1 on: April 03, 2003, 05:34:09 PM »
Sure but not so elegant solution:

p = &str[0][0];
1. Make an announcment.
2. Wait a while.
3. Check if it can actually be done.
4. Wait for someone else to do it.
5. Start working on it while giving out hillarious progress-reports.
6. Deny that you have ever announced it
7. Blame someone else
 

  • Guest
Re: I need help with C(++)
« Reply #2 on: April 03, 2003, 06:10:56 PM »
Quote

z5 wrote:
#include
#include
#include

void main(void)
  {
     const numberofstrings = 5;
     char str[numberofstrings][80],*p;
       
     for(p=str;p                {
                printf("Type string: ");
                gets(p);
                }
}


This won't work, even if you could force the assignment of p to the beginning of your array.  p is a char *.   p++ points to the char immediately following the char first pointed at by p.  To get p++ to skip 80 characters and point to the next string, you need to declare p as a pointer to an array of char[80] like this:

char (*p)[80];

I would never code it this way myself though.  To loop through arrays of strings I would use:

for (i = 0; i < numberofstrings; i++) {
   printf("Type string:  ");
   gets(str);
}

Since str is an array of character arrays, str
  • can be used as a pointer to a char, and this way the array notation takes care of the pointer arithemetic.


MikeS
 

  • Guest
Re: I need help with C(++)
« Reply #3 on: April 03, 2003, 06:14:05 PM »
Or better, use p = str[0];
Then in your loop, you'll need to use p+= 80.

--
MikeS
 

Offline Karlos

  • Sockologist
  • Global Moderator
  • Hero Member
  • *****
  • Join Date: Nov 2002
  • Posts: 16879
  • Country: gb
  • Thanked: 5 times
    • Show only replies by Karlos
Re: I need help with C(++)
« Reply #4 on: April 03, 2003, 09:12:18 PM »
If it's a c++ project, you could just use the string class :-)
int p; // A
 

Offline Karlos

  • Sockologist
  • Global Moderator
  • Hero Member
  • *****
  • Join Date: Nov 2002
  • Posts: 16879
  • Country: gb
  • Thanked: 5 times
    • Show only replies by Karlos
Re: I need help with C(++)
« Reply #5 on: April 03, 2003, 09:27:29 PM »
int p; // A
 

Offline Karlos

  • Sockologist
  • Global Moderator
  • Hero Member
  • *****
  • Join Date: Nov 2002
  • Posts: 16879
  • Country: gb
  • Thanked: 5 times
    • Show only replies by Karlos
Re: I need help with C(++)
« Reply #6 on: April 04, 2003, 09:47:31 AM »
One last point,

It's generally not a good idea to declare arrays 'on the stack' like this.

You could make the array static, or allocate space with malloc (or new[] if using C++)

Also, my last example was a bit wrong in that you get the return character as the last char of your string. To fix this you get the number of chars read from fgets() and set the last character-1 to 0

eg
for (...)
{
   int len;
   printf("Type string: ");
   len = fgets(p->data, bufferSize-1, stdin);
   p->data[len-1] = '\0';
}
int p; // A
 

Offline Piru

  • \' union select name,pwd--
  • Hero Member
  • *****
  • Join Date: Aug 2002
  • Posts: 6946
    • Show only replies by Piru
    • http://www.iki.fi/sintonen/
Re: I need help with C(++)
« Reply #7 on: April 04, 2003, 10:34:38 AM »
@Karlos

Just being pedantic here, but:

a) If the stream end in immeditate EOF, your code trash memory *before* the buffer [0-1]. Not good.

b) The last char is not always a newline. If the stream ends (EOF) you now kill the last perfectly valid char. Not good.

Make it:

/* If the last char is a newline, kill it */
if (len && p->data[len-1] == '\n')
{
  p->data[len-1] = '\0';
}

/* Note to anyone who thinks the char could be '\r' too: PC stdio libs have automagic "\r\n" -> "\n" at input and automagic "\n" -> "\r\n" at output. Or at least should have. :-) */
 

Offline Karlos

  • Sockologist
  • Global Moderator
  • Hero Member
  • *****
  • Join Date: Nov 2002
  • Posts: 16879
  • Country: gb
  • Thanked: 5 times
    • Show only replies by Karlos
Re: I need help with C(++)
« Reply #8 on: April 04, 2003, 10:53:02 AM »
@Piru

Thanks for that, you are completely correct. Everybody ignore me, I'm not fully awake :-)

I just posted without actually checking the behaviour of fgets() - I did edit my post just now but forgot to check since I'm at work mucking about with god awful vbscript (ewww!)

Anhow, listen to the guy, he knows what he's talking about!
int p; // A
 

Offline Karlos

  • Sockologist
  • Global Moderator
  • Hero Member
  • *****
  • Join Date: Nov 2002
  • Posts: 16879
  • Country: gb
  • Thanked: 5 times
    • Show only replies by Karlos
Re: I need help with C(++)
« Reply #9 on: April 04, 2003, 10:58:54 AM »
Right, from the top :-D


#include
#include
#include

/* create a string buffer type */

const size_t bufferSize = 80;

typedef struct {
   char data[bufferSize];
} FixedStringBuffer;

void main(void)
{
   const size_t numberofstrings = 5;
   FixedStringBuffer strBuf[numberofstrings], *p;
       
   for(p=strBuf;p   {
      int len;
      printf("Type string: ");
      len = fgets(p->data, bufferSize-1, stdin);
      if (len && p->data[len-1] == '\n')
         p->data[len-1] = '\0';
   }
}

int p; // A
 

Offline z5Topic starter

  • Sr. Member
  • ****
  • Join Date: May 2002
  • Posts: 366
    • Show only replies by z5
    • http://ada.untergrund.net
Re: I need help with C(++)
« Reply #10 on: April 04, 2003, 11:59:33 AM »
my teacher will never believe that i coded this myself  :-D
A.miga D.emoscene A.rchive: Relive the dreams...
 

Offline Karlos

  • Sockologist
  • Global Moderator
  • Hero Member
  • *****
  • Join Date: Nov 2002
  • Posts: 16879
  • Country: gb
  • Thanked: 5 times
    • Show only replies by Karlos
Re: I need help with C(++)
« Reply #11 on: April 04, 2003, 12:00:57 PM »
It's got pointers, a structure and safety bound IO. What more does s/he want ?

:-)

-edit-

As long as you follow it, you can explain it if they get suspicious :-D
int p; // A
 

Offline z5Topic starter

  • Sr. Member
  • ****
  • Join Date: May 2002
  • Posts: 366
    • Show only replies by z5
    • http://ada.untergrund.net
Re: I need help with C(++)
« Reply #12 on: April 05, 2003, 05:40:56 PM »
ok, i've got another problem:

#include
#include

void main(void)
{
int aantalmaaltijden, jarenklant, prijsmaaltijd, prijsmaxmaaltijd, korting;
int kortingonderdrie, kortingbovendrie;
float totalebedrag;
char klant[80];

printf("Geef de prijs van 1 maatijd: ");
scanf("%d",&prijsmaaltijd);
printf("Geef de prijs van 5 maaltijden: ");
scanf("%d",&prijsmaxmaaltijd);
printf("Geef de korting bij minder dan 3 jaar klant: ");
scanf("%d",&kortingonderdrie);
printf("Geef de korting als 3 of meer jaar klant: ");
scanf("%d",&kortingbovendrie);
printf("Geef de naam van de klant: ");
gets(klant);
printf("Geef het aantal maaltijden: ");
scanf("%d",&aantalmaaltijden);
printf("Geef aantal jaren klant: ");
scanf("%d",&jarenklant);
}

Don't mind all the text (it's dutch). The problem is the gets(klant). If i run the program, the program doesn't wait for something to be typed on the keyboard and jumps straight to the next printf.

Any ideas?
Thanks
A.miga D.emoscene A.rchive: Relive the dreams...
 

Offline Karlos

  • Sockologist
  • Global Moderator
  • Hero Member
  • *****
  • Join Date: Nov 2002
  • Posts: 16879
  • Country: gb
  • Thanked: 5 times
    • Show only replies by Karlos
Re: I need help with C(++)
« Reply #13 on: April 05, 2003, 05:43:58 PM »
There's probalbly some newline chars or whatever left in the input buffer after scanf()

Try sticking fflush(stdin) before your gets() and see if that helps.

int p; // A
 

Offline z5Topic starter

  • Sr. Member
  • ****
  • Join Date: May 2002
  • Posts: 366
    • Show only replies by z5
    • http://ada.untergrund.net
Re: I need help with C(++)
« Reply #14 on: April 05, 2003, 05:47:44 PM »
bingo  :-o

You're my hero Karlos  :-D

Seriously though, you seem to know a lot about programming, yet reading some threads i have the impression that you don't do it for a living? Why not?
A.miga D.emoscene A.rchive: Relive the dreams...