Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 135,958 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,605 people online right now. Registration is fast and FREE... Join Now!




newbieproblems

 
Reply to this topicStart new topic

newbieproblems, c++ question about a school project

kroustou
10 May, 2008 - 08:42 AM
Post #1

New D.I.C Head
*

Joined: 10 May, 2008
Posts: 6

hi everyone,im a total niewbie,in c++ and i have a couple of problems while doing a project i was asked for school.
im supposed to make o program that makes couples confused.gif!
there are 2 types of men and women a and a' and g and g'...."a" likes more "g'" and "g'"likes more "a"...and so..ive made this and i have compilations problems....:
CODE


#include <stdio.h>
#include <iostream>

using name space std;

char man[10],woman[10],a;//variables (a is the type om man and woman)
int couples;//integer-the number of the couples i want to have

bool marriage(char a[10],char g[10],int ga,int gg)
    {
    if ( ga + gg == 1)
        return true;
    }


int main()//main marriage :P
{    int womanType[couples];
    printf("how many couples?\n");
    cin >> couples;//input o fthe nuber of couples
    for (int i = 0; i < couples; i++)
        {
        char a;
        
        
        cout << "type womans name"<<i;
        cin >>woman[i];
        cout << "type the type of "<<woman[i]<<"?"<<endl;
        cin >> a;
        if ( a = "g")
             womanType[i] = 1;//type g
        else
             womanType[i] = 0;//type g'
        }
    for (int i=0; i<couples; i++)
        {int manType[couples];
        cout << "type mans name"<<i;
        cin >> man[i];
        cout << "man type?";
        cin >> a;
        if ( a = "a")
            manType[i] = 1;//type a
        else
            manType[i] = 0;//type a'
        }
    int manType[couples];
    bool couple[couples][couples];
    for (int i = 0; i <couples; i++)
        for (int j = 0; j <couples; j++)
            couple[i][j] = marriage(man[i],woman[j],manType[i],womanType[j]);
    for (int i = 0; i <= couples; i++)
        for (int j = 0; j <= couples; j++)
            if (couple[i][j]==true)
                cout << man[i]<<"and"<<woman[j]<<"make good couple!"<<endl;
}
//end of making couples    

im writting it in linux and the compilers results are:
line 28:invalid conversion from const char to *char
line 39: >> >>
line 48:initializing argument 2 of 'bool padrema(char*,char*,int,int)
i suppose that i have made pointers without actually wanting it :s...
how can i make compilation work?
i have included iostream.h because with iostream it wouldnt recognize cin and cout,dont know why sad.gif...
any help would be appreciated...thanks in advance
ps:the comments and the project are in greek if u want i can change it....

This post has been edited by kroustou: 10 May, 2008 - 11:06 AM
User is offlineProfile CardPM
+Quote Post

Renzokusen
RE: Newbieproblems
10 May, 2008 - 09:51 AM
Post #2

New D.I.C Head
*

Joined: 11 Mar, 2008
Posts: 32

I'd suggest making better variables names maybe ones that are self documenting. I get a headache just looking at your code. While variables are all the same regardless of their name having self documenting variables helps others understand your code better. If you need two type of men and two type of women what's wrong with using manOne & manTwo or firstManType & secondManType. Just an idea.
User is offlineProfile CardPM
+Quote Post

kroustou
RE: Newbieproblems
10 May, 2008 - 10:09 AM
Post #3

New D.I.C Head
*

Joined: 10 May, 2008
Posts: 6

QUOTE(Renzokusen @ 10 May, 2008 - 10:51 AM) *

I'd suggest making better variables names maybe ones that are self documenting. I get a headache just looking at your code. While variables are all the same regardless of their name having self documenting variables helps others understand your code better. If you need two type of men and two type of women what's wrong with using manOne & manTwo or firstManType & secondManType. Just an idea.

thats what i did but its in greek
and the type of man or woman should be inserted after the name...so its tha name
and then the type a or a'...i just dont know what should i do whith the compilation problems....
User is offlineProfile CardPM
+Quote Post

silv3rback
RE: Newbieproblems
10 May, 2008 - 10:18 AM
Post #4

New D.I.C Head
*

Joined: 10 May, 2008
Posts: 5

Hi,

Quick thoughts, haven't tested your code.

<iostream.h> has been deprecated, if you want to use cin and cout with <iostream> add this line under your headers:
CODE

using namespace std;


Line 28: your if condition looks like an assignment, is this your intention? If you are comparing use ==

I'm also very new at this, but if you can translate your code or add comments in english, I might be able to give you a little bit more help.

Hth

User is offlineProfile CardPM
+Quote Post

kroustou
RE: Newbieproblems
10 May, 2008 - 10:20 AM
Post #5

New D.I.C Head
*

Joined: 10 May, 2008
Posts: 6

QUOTE(silv3rback @ 10 May, 2008 - 11:18 AM) *

Hi,

Quick thoughts, haven't tested your code.

<iostream.h> has been deprecated, if you want to use cin and cout with <iostream> add this line under your headers:
CODE

using namespace std;


Line 28: your if condition looks like an assignment, is this your intention? If you are comparing use ==

I'm also very new at this, but if you can translate your code or add comments in english, I might be able to give you a little bit more help.

Hth

oh yes forgot thank you
ill post it with english variables and comments!
oh and if i do == the compiler tells me that c++ forbids comparison between a char and and an integer....

This post has been edited by kroustou: 11 May, 2008 - 02:40 AM
User is offlineProfile CardPM
+Quote Post

DominationXVI
RE: Newbieproblems
10 May, 2008 - 03:39 PM
Post #6

New D.I.C Head
*

Joined: 2 Feb, 2008
Posts: 36

You cannot create an array this way:

CODE
int womanType[couples];


because the variable couples has not yet been given a value. If you want to get the size of the array at run-time, you must declare storage for it dynamically.

CODE


cout << "how many couples: ";
cin >> couples;

int *ptr = new int[couples];



User is offlineProfile CardPM
+Quote Post

kroustou
RE: Newbieproblems
11 May, 2008 - 03:04 AM
Post #7

New D.I.C Head
*

Joined: 10 May, 2008
Posts: 6

what i want to do is to make man,woman and a characters....Can anyone help me?
the compilers see them as pointers....

This post has been edited by kroustou: 11 May, 2008 - 03:19 AM
User is offlineProfile CardPM
+Quote Post

silv3rback
RE: Newbieproblems
11 May, 2008 - 07:19 AM
Post #8

New D.I.C Head
*

Joined: 10 May, 2008
Posts: 5

I've not corrected your code, I've put my comments inline, I think learning how to fix this might be a valuable experience. My comments have '##' before the comment. See my recommendations after the code box:
CODE
#include <stdio.h>
#include <iostream>

using name space std;
/* ##correct this to using namespace std , namespace is a single word */

char man[10],woman[10],a;//variables (a is the type om man and woman)

int couples;//integer-the number of the couples i want to have
/*## This does not need to be global*/

/*## No prototype for this function, good habit to declare the prototype first*/

bool marriage(char a[10],char g[10],int ga,int gg)
    {
    if ( ga + gg == 1)
        return true;
    }


int main()//main marriage :P
{  
  int womanType[couples];
  /*## This is not a valid declaration, either use a pointer to dynamically
    allocate memory for the array, or use a constant value*/
  printf("how many couples?\n");
  cin >> couples;//input of the number of couples
  for (int i = 0; i < couples; i++)
    {
      /*##I don't think this is what you want
    this will force you to type in ten characters for ONE womans name*/
      cout << "type womans name"<<i;
      cin >>woman[i];
      cout << "type the type of "<<woman[i]<<"?"<<endl;
      cin >> a;
      if ( a == 'g') /*## I think you have to use single quotes ' */
      womanType[i] = 1;//type g
        else
      womanType[i] = 0;//type g'
        }
  //same for comments for women apply to men
  for (int i=0; i<couples; i++)
    {int manType[couples];/*## You are declaring this each time inside
                the loop! */
      cout << "type mans name"<<i;
      cin >> man[i];
      cout << "man type?";
      cin >> a;
      if ( a = "a")
    manType[i] = 1;//type a
      else
    manType[i] = 0;//type a'
    }
  int manType[couples]; /*## In general its a good idea to put all
              declarations in the same function at the front
              just so that your code is nicely organized*/
  bool couple[couples][couples];
  for (int i = 0; i <couples; i++)
    for (int j = 0; j <couples; j++)
      couple[i][j] = marriage(man[i],woman[j],manType[i],womanType[j]);
  /*## you are passing a single character of man and woman as against the
     entire string (which I think you are trying to do.
  */
  for (int i = 0; i <= couples; i++)
    for (int j = 0; j <= couples; j++)
      /*## using <= will search outside the bounds of your array! */
      if (couple[i][j]==true)
    cout << man[i]<<"and"<<woman[j]<<"make good couple!"<<endl;
  /*##Again this will output the 'i'th character of the man's name and
    the 'j'th character of the woman's name if it is true */
}
//end of making couples    


I think you're trying to create 10 women and men strings, at the moment it looks like you're only creating one woman and man string with 10 characters. You need to restructure your variables to allow 10 instances of man and woman. Also have a quick read on passing arrays(or multidimensional arrays) to functions.
Take a shot at it and let us know!
User is offlineProfile CardPM
+Quote Post

kroustou
RE: Newbieproblems
11 May, 2008 - 07:41 AM
Post #9

New D.I.C Head
*

Joined: 10 May, 2008
Posts: 6

ok thank you,iv corrected some problems line namespace etc and i have seen that i have put <= in the loops,about the names of men and women i try to enter the name which will be 10 characters long for every man and the type,i think its correct thank you ill check the other problems and i ll inform you!oh and i didnt understand about the prototype you said before the function...

This post has been edited by kroustou: 11 May, 2008 - 07:42 AM
User is offlineProfile CardPM
+Quote Post

kroustou
RE: Newbieproblems
11 May, 2008 - 08:11 AM
Post #10

New D.I.C Head
*

Joined: 10 May, 2008
Posts: 6

i did the corrections and i made pointers the man and woman
and i have succesfull compilation
when i run it
the time that i have to put the womans name it corrupts and outputs an error:
segmentation fault(core dumped)
-----------------------------------------
(program exited with code: 139)
Press return to continue
thats the code made ti work:
CODE

#include <stdio.h>
#include <iostream>

using namespace std;

char *man[10],*woman[10],a;//variables (a is the type om man and woman)

/*## No prototype for this function, good habit to declare the prototype first*/

bool marriage(char a[10],char g[10],int ga,int gg)
    {
    if ( ga + gg == 1)
        return true;
    }


int main()//main marriage :P
{  
  int couples;//couples:integer-the number of the couples i want to have
  /*## This is not a valid declaration, either use a pointer to dynamically
    allocate memory for the array, or use a constant value*/
  printf("how many couples?\n");
  cin >> couples;//input of the number of couples
  int womanType[couples];
  for (int i = 0; i < couples; i++)
    {
      /*##I don't think this is what you want
    this will force you to type in ten characters for ONE womans name*/
      cout << "type womans name"<<i;
      cin >>woman[i];
      cout << "type the type of "<<woman[i]<<"?"<<endl;
      cin >> a;
      if ( a == 'g') /*## I think you have to use single quotes ' */
      womanType[i] = 1;//type g
        else
      womanType[i] = 0;//type g'
        }
  //same for comments for women apply to men
  for (int i=0; i<couples; i++)
    {int manType[couples];/*## You are declaring this each time inside
                the loop! */
      cout << "type mans name"<<i;
      cin >> man[i];
      cout << "man type?";
      cin >> a;
      if ( a = 'a')
    manType[i] = 1;//type a
      else
    manType[i] = 0;//type a'
    }
  int manType[couples]; /*## In general its a good idea to put all
              declarations in the same function at the front
              just so that your code is nicely organized*/
  bool couple[couples][couples];
  for (int i = 0; i <couples; i++)
    for (int j = 0; j <couples; j++)
      couple[i][j] = marriage(man[i],woman[j],manType[i],womanType[j]);
  /*## you are passing a single character of man and woman as against the
     entire string (which I think you are trying to do.
  */
  for (int i = 0; i <= couples; i++)
    for (int j = 0; j <= couples; j++)
      /*## using <= will search outside the bounds of your array! */
      if (couple[i][j]==true)
    cout << man[i]<<"and"<<woman[j]<<"make good couple!"<<endl;
  /*##Again this will output the 'i'th character of the man's name and
    the 'j'th character of the woman's name if it is true */
}
//end of making couples    


This post has been edited by kroustou: 11 May, 2008 - 08:15 AM
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/1/08 09:21AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month