Weird symbols in char array output

For some reason this code outputs some weird symbols after I enter any letter into the char array.

MovieData.h
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#ifndef MOVIEDATA_H
#define MOVIEDATA_H
#include <cstring>
const int SIZE=50;

class MovieData{
private:
	char title[SIZE];
	char director[SIZE];
	int yearRls;
	int runTime;
	int cost;
	int revenue;
public: 
	void setTitle(char t[]) {strcpy(title, t);} 
	void setDirector(char d[]) {strcpy(director, d);} 
	void setYear(int y){yearRls = y;}
	void setrunTime(int rt) {runTime = rt;}
	void setCost(int c) {cost = c;}
	void setRevenue(int r) {revenue = r;}
	 
	const char * getTitle(){return title;}
	const char * getDirector(){return director;}
	int getYear(){return yearRls;}
	int getrunTime() {return runTime;}
	int getCost() {return cost;}
	int getRevenue() {return revenue;}
	void print() {
		cout<<endl<<endl;
		cout<<"-------------------------------------------"<<endl;
		cout<<"\t\t Movie Data       "<<endl;
		cout<<"-------------------------------------------"<<endl;
		cout<<"Movie Title                | "<<title<< endl;;
		cout<<"                           | "<<endl;
		cout<<"Director                   | "<<director<<endl;
		cout<<"                           | "<<endl;
		cout<<"Release Year               | "<<yearRls<<endl;
		cout<<"                           | "<<endl;
		cout<<"Movie Runtime in minutes   | "<<runTime<<endl;
		cout<<"-------------------------------------------"<<endl;
		cout<<endl;
		ProfitOrLoss();
		cout<<"-------------------------------------------"<<endl;
	}

	void ProfitOrLoss(){
		int pol = revenue - cost;
		if(cost < revenue){
			cout<<endl;
			cout<<"First years profit: "<< pol <<endl;
		}
		if(cost > revenue){
			cout<<endl;
			cout<<"First years loss: "<< pol <<endl;
		}
	}
};
#endif


Movie_main.cpp
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
#include<iostream>
using namespace::std;
#include "MovieData.h"

void getMovieData(MovieData movieArray[], int size);
void getCostRev(MovieData movieArray[], int size);
void printAllMovieData(MovieData movieArray[], int size);

int main(){

	MovieData movie[SIZE];
	int n;

	cout<<"Entre la cantidad de peliculas: ";
	cin >>n;

	getMovieData(movie, n);

	
	getCostRev(movie, n);

	printAllMovieData(movie, n);
	cin.get();

	return 0;
}


Movie.cpp
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include<iostream>
using namespace::std;
#include "MovieData.h"

void getMovieData(MovieData mov[], int size){
	
	char temp[SIZE];
	int tmp;
	for(int i=0;i<size;i++){
		temp[0]='\0';
		tmp = 0;
		cin.ignore();
		cout<<endl;
		cout<<"Enter the title of Movie #"<<i+1<<": ";
		cin.getline(temp,SIZE,'\n');
		mov[i].setTitle(temp);

		cout<<"Enter the name of the director: ";
		cin.getline(temp,SIZE,'\n'); 
		mov[i].setDirector(temp);

		cout<<"Enter the release date of the movie: ";
		cin>> tmp;
		mov[i].setYear(tmp);

		cout<<"Enter the running time of the movie in minutes: ";
		cin>> tmp;
		mov[i].setrunTime(tmp);
	}//end for
	
}


void getCostRev(MovieData mov[], int size){
	int temp;
	for(int i=0;i<size;i++){
	cout<<endl;
	cout<<"What were the costs of"<<endl;
	cout<<"the movie "<<mov[i].getTitle()<<" ?: ";
	cin>>temp;
	mov[i].setCost(temp);
	cout<<"What were its revenues?: ";
	cin>>temp;
	mov[i].setRevenue(temp);
	}
}//end getCostRev

void printAllMovieData(MovieData movieArray[], int size){
	for (int i=0; i<size; i++){
		movieArray[i].print();
	}
}//end printAllMovieData 


Resolved
Last edited on
There are a number of issues with this code. But let's begin with the member variables:
1
2
	char title;
	char director;


Each of these is just a single character. You could start by changing them to std::string or perhaps a character array.

I originally had the string library, which worked perfectly, but unfortunately I'm not allowed to use it for this particular program. I also changed them to character arrays but I still got the same output.
Last edited on
Well, a character array would work. A single character wouldn't.

Could you show the version of the code where you use character arrays please.
Yes, sorry I didn't say I had already changed the code on top of the page. And having those member values as character arrays makes it display the same symbol but more of them.
Last edited on
Sure, I understand that. But it's not possible to say what the problem is without seeing the rest of the code.

Did you only change two lines of the program, or were there other changes?
I didn't make any other changes. And all of the code was already posted.
Last edited on
Lots of changes are required.
#include <cstring> // for strcpy

old:
 
    void setTitle(char t){title[SIZE] = t;}

new:
 
    void setTitle(char * t) { strcpy(title, t);}


old:
 
    char getTitle(){return title[SIZE];}

new:
 
    const char * getTitle(){return title;}


old:
1
2
    cin.getline(temp,'\n')
    mov[i].setTitle(temp[SIZE]);

new:
1
2
    cin.getline(temp, SIZE, '\n');
    mov[i].setTitle(temp);


And so on... that should be enough to get you started in the right direction.

Last edited on
Thanks, got it to work with your help. Just edited the code on the top of how I got it to work.
Last edited on
Topic archived. No new replies allowed.