12 Replies - 6114 Views - Last Post: 11 July 2011 - 11:52 AM Rate Topic: -----

#1 martinmax3  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 23-June 11

Structure required on left side of . or .*

Posted 11 July 2011 - 04:04 AM

I got this code from my teacher, I do not know what compiler she used, but I get this strange error and I can't find solution anywhere.

Error at compile: Structure required on left side of . or .*

Line of error:
c.erase(0,4);


Full code:
#include<iostream.h>
#include<string.h>
struct talanti
{
	char name[46];
	char kod[11];
	char vid[21];
	int tochki;
}hora[500];
int n;//broj hora
void vhod()
{
	do
	{
		cout<<"n=";
		cin>>n;
	}
	while(n<1||n>500);
	for(int i=0;i<n;i++)
	{
		cin.get();
		cout<<"vavedi palno ime na uchastnika->";
		cin.getline(hora[i].name,46);
		strtok(hora[i].name,"\n");
		cout<<"kod na uchastnika";
		cin.getline(hora[i].kod,11);
		cout<<"vavedi vid na talanta";
		cin.getline(hora[i].vid,21);
		cout<<"vavedi tochki";
		cin>>hora[i].tochki;
	}
}
void izod1()
{
	char *n1,*n2,*n3,ip[46];
	char a[4],b[5];
	char c[11];
	int rd[6],god,mec,den;
	for(int i=0;i<n;i++)
	{
		strcpy(ip,hora[i].name);
		n1=strtok(ip," ");//nasocva n1 kam parvoto ime
		n2=strtok(NULL," "); //nasochva n2 kam vtoroto ime
		n3=strtok(NULL," "); //nasochva n3 kam tretoto ime
		strncpy(a,hora[i].kod,3);
		strncpy(b,hora[i].kod,4);
		strcpy(c,hora[i].kod);
		c.erase(0,4);
		for(int j=0;j<n;j++) rd[j]=c[j]-'0';
		den=rd[0]*10+rd[1];
		mec=rd[2]*10+rd[3];
		god=rd[4]*10+rd[5];
		cout<<n1<<""<<n2[0]<<"."<<n3<<","<<""<<b[4]<<","<<hora[i].vid<<","<<den<<"."<<mec<<"."<<god<<endl;
	}
}
int main()
{
	vhod();
	izod1();
	return 0;
}


P.S. I am using Borland C++ v3.1

This post has been edited by martinmax3: 11 July 2011 - 04:05 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Structure required on left side of . or .*

#2 simeesta  Icon User is offline

  • Deadly Ninja


Reputation: 218
  • View blog
  • Posts: 591
  • Joined: 04-August 09

Re: Structure required on left side of . or .*

Posted 11 July 2011 - 04:09 AM

char c[11] is a character array. If you want to use c.erase, c must be a string. ie string c .

erase

This post has been edited by simeesta: 11 July 2011 - 04:11 AM

Was This Post Helpful? 0
  • +
  • -

#3 PlasticineGuy  Icon User is offline

  • mov dword[esp+eax],0
  • member icon

Reputation: 281
  • View blog
  • Posts: 1,436
  • Joined: 03-January 10

Re: Structure required on left side of . or .*

Posted 11 July 2011 - 04:16 AM

Tell your teacher to get a newer compiler, hers is a piece of crap. Anything that will accept #include <iostream.h> is old and unreliable -- it should be #include <iostream> (yes, it's important).
Was This Post Helpful? 0
  • +
  • -

#4 martinmax3  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 23-June 11

Re: Structure required on left side of . or .*

Posted 11 July 2011 - 04:29 AM

@PlasticineGuy: That's not helping me. This code is for a task for the college I will hold exam for. It is written like that (with the .h) in a C++ book by a teacher from that college.

@Simeesta: I can't declare string, it won't let me. It just doesn't make it bold when I write it like it does with the other types and also it gives error.

Isn't there a function like erase, which works for this type?
Was This Post Helpful? 0
  • +
  • -

#5 simeesta  Icon User is offline

  • Deadly Ninja


Reputation: 218
  • View blog
  • Posts: 591
  • Joined: 04-August 09

Re: Structure required on left side of . or .*

Posted 11 July 2011 - 04:33 AM

No. Did you #include <string>? <string.h> is <cstring> which is different to #include <string>. Again you should get a newer compiler.
Was This Post Helpful? 1
  • +
  • -

#6 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: Structure required on left side of . or .*

Posted 11 July 2011 - 05:21 AM

*
POPULAR

@martinmax3 -- The code you have is just wrong as there is nothing you can really do with that like c.erase(0,4);. Comment it out and try to work though the code to figure out what it was doing there.

The dot operator here is trying to call a "member function" called "erase", the problem is that c is a char array and char arrays do not have member functions, it must be a class or struct etc. to have a member function.


As for the compiler. Your so called "college" is ripping you off if it is using a book that uses TurboC/Borland 3.1 -- that was my very first compiler and I do have much love for it, but it is 20 years old and pre-dates modern C++ by quite a bit.

It is horrible that your school is still using something that old because technology moves pretty fast. Imagine taking an an English class that used Elizabethan English to teach. Sure there are wonderful reasons to do this, it will ensure you have a better understanding of Shakespeare! But it also means that you will not understand, and have a difficult time speaking to users of the modern language.

It is pretty much the same thing with this... except maybe instead of Elizabethan english we are referring to 80's slang.

When you graduate you are going to have to unlearn all the crap they taught you in school and re-learn modern programming.

the thing is. Most modern compilers are free. And it is actually kind of hard to find a book that covers that old C/C++ -- so your school is actively ripping you off. They are conspiring with some book publisher / software vendor to charge you to learn 20 year old techniques. They are literally crooks and you are letting them get away with it.

Your compiler will not even run on modern 64bit computers!!! Your school is wasting your time and money!

But the thing is: you are the customer.

Do yourself a favor: Get a new compiler (they are free). Start to learn modern programming rather than anachronistic Turbo-C from the 1980's. It will not cost you anything but a little time and effort.
Was This Post Helpful? 6
  • +
  • -

#7 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: Structure required on left side of . or .*

Posted 11 July 2011 - 05:27 AM

Oh and if you are in India I am sorry. The corruption there seems to run pretty deep on this issue. The author of the book and publishers are apparently on the board that decides what books get used in state-run facilities? Anyway I don't know the detail but from a outside perspective it is pretty crazy how many people are paying for and getting a crappy education because of that.
Was This Post Helpful? 0
  • +
  • -

#8 martinmax3  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 23-June 11

Re: Structure required on left side of . or .*

Posted 11 July 2011 - 05:42 AM

So I got MS Visual C++ 2010 Express and still this string type does not work. I am probably messing up something.

I am not from India, I am from Bulgaria. I am not exactly sure about this book I saw the code in, it might be old and that's why the code is outdated. But the program I use for C++ (Borland C++ 3.1) was what I studied in my school for C++, I downloaded my copy from the website of the biggest Technical University in my country, which is in our capital.

So I will just hope I get lucky and qualify, because if I don't I might have to go to a even worse college. People from this "worse" college came to our school and probably went to a lot of other schools, and let us cheat and talk to each other. And also to be even more sure we will get good marks before the test starts they saed that the grading system is such that there is no way for someone to get less than 4 (probably it's C your mark system). So it's like if you hold this exam you qualify for sure. They must be in desperate need for students.
Was This Post Helpful? 0
  • +
  • -

#9 PlasticineGuy  Icon User is offline

  • mov dword[esp+eax],0
  • member icon

Reputation: 281
  • View blog
  • Posts: 1,436
  • Joined: 03-January 10

Re: Structure required on left side of . or .*

Posted 11 July 2011 - 05:45 AM

Sounds highly corrupt.
I would recommend to continue studying at your current college, but also in your own time to download a good free compiler (like Visual C++ 2010 or Code::Blocks), and learn modern C++ via online tutorials.

The reason string doesn't go bold is because it's not a built in data type -- it's a class, a custom data type, kind of like cout.

This post has been edited by PlasticineGuy: 11 July 2011 - 05:46 AM

Was This Post Helpful? 1
  • +
  • -

#10 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6039
  • View blog
  • Posts: 23,436
  • Joined: 23-August 08

Re: Structure required on left side of . or .*

Posted 11 July 2011 - 06:34 AM

Posted Image
Was This Post Helpful? 4
  • +
  • -

#11 martinmax3  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 23-June 11

Re: Structure required on left side of . or .*

Posted 11 July 2011 - 08:54 AM

@JackOfAllTrades :D

Edit: NVM

This post has been edited by martinmax3: 11 July 2011 - 09:16 AM

Was This Post Helpful? 0
  • +
  • -

#12 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: Structure required on left side of . or .*

Posted 11 July 2011 - 09:32 AM

So here is a more updated version of your code:
#include<iostream>
#include<cstring>

using namespace std;

struct talanti {
    char name[46];
    char kod[11];
    char vid[21];
    int tochki;
} hora[500];

int n;//broj hora

void vhod() {
    do {
        cout << "n=";
        cin >> n;
    } while(n < 1 || n > 500);

    for(int i = 0; i < n; i++) {
        cin.get();
        cout << "vavedi palno ime na uchastnika->";
        cin.getline(hora[i].name, 46);
        strtok(hora[i].name, "\n");
        cout << "kod na uchastnika";
        cin.getline(hora[i].kod, 11);
        cout << "vavedi vid na talanta";
        cin.getline(hora[i].vid, 21);
        cout << "vavedi tochki";
        cin >> hora[i].tochki;
    }
}
void izod1() {
    char *n1, *n2, *n3, ip[46];
    char a[4], b[5];
    char c[11];
    int rd[6], god, mec, den;

    for(int i = 0; i < n; i++) {
        strcpy(ip, hora[i].name);
        n1 = strtok(ip, " "); //nasocva n1 kam parvoto ime
        n2 = strtok(NULL, " "); //nasochva n2 kam vtoroto ime
        n3 = strtok(NULL, " "); //nasochva n3 kam tretoto ime
        strncpy(a, hora[i].kod, 3);
        strncpy(b, hora[i].kod, 4);
        strcpy(c, hora[i].kod);

        //c.erase(0,4);
        for(int j = 0; j < n; j++) rd[j] = c[j] - '0';

        den = rd[0] * 10 + rd[1];
        mec = rd[2] * 10 + rd[3];
        god = rd[4] * 10 + rd[5];
        cout << n1 << "" << n2[0] << "." << n3 << "," << "" << b[4] << "," << hora[i].vid << "," << den << "." << mec << "." << god << endl;
    }
}
int main() {
    vhod();
    izod1();
    return 0;
}



it compiles fine -- the line c.erase(0,4) still throws an error but I don't see that it really should make a difference in the logic anyway... I think it was probably some kind of type-o.

perhaps the purpose was to remove the first 4 chars of the string in the array c... so not a problem really you can just do a char* cc = c+4; and cc will point to a the correct place in the string.

however this part concerns me:

for(int j = 0; j < n; j++) rd[j] = c[j] - '0';


now [il]rd[j] = c[j] - '0'[/code] -- this is a typical scheme for converting char digits into numeric values: '7' - '0' = 7

so that part is ok... but look at the loop. rd is 6 ints long... n is the number of values in the array hora. So if the user enters 100 records in hora... then that little loop will zoom past the end of rd...

I think that should read:

for(int j = 0; j < 6; j++) rd[j] = c[j] - '0';



so if we take that all together:
        //c.erase(0,4); // assuming this means remove the first 4 chars...
        for(int j = 0; j < 6; j++) rd[j] = c[j] - '0';


would be the same as:
        for(int j = 0; j < 6; j++) rd[j] = c[j+4] - '0';


of course I don't speak the language so I can't really tell what the intent of these lines of code are but I think you should be able to talk yourself though the logic.
Was This Post Helpful? 3
  • +
  • -

#13 martinmax3  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 23-June 11

Re: Structure required on left side of . or .*

Posted 11 July 2011 - 11:52 AM

Thanks so much, Nick! It does what it is supposed to now and runs, but it gives out strange numbers at the end. The good thing is I now know how to do this, it won't be the same task, but it will be like it.

Quote

now [il]rd[j] = c[j] - '0'[/code] -- this is a typical scheme for converting char digits into numeric values: '7' - '0' = 7

This is really good to know. Will save me so much failing with the convertions. :D

I won't bother you guys no more. My exam is at 13th so fingers crossed. :D
Again Thanks so much to everyone that helped me. This is really one great forum.

Thank You! :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1