What is wrong with this c++ code?

Wing0

Estimable
Nov 5, 2014
47
0
4,590
1
Code:
#include "StdAfx.h"
#include <iostream>
#include <string>

using namespace std;

int main()
{
	string movie;
	cout << "What is the name of the movie? \n";
	getline(cin, movie);

	int metescore;
	cout << "what is the metascore for " << movie << "? \n";
		cin >> metescore;

		int tomato;
		cout << "What is the tomatoscore for " << movie << "? \n";
		cin >> tomato;
		int av;
		int avg;
			tomato + metescore = av;
		av / 2 = avg;

			cout << "The average score for " << movie << " is " << avg << ".";
}
I get the error
Code:
1>c:\users\brady\documents\visual studio 2010\projects\program3\program3\program3.cpp(22): error C2106: '=' : left operand must be l-value
2 times.
 

edavidzar

Estimable
Mar 25, 2015
1
0
4,520
1
I think that "tomato + metescore = av;" is wrong because you can not assign av to a sum of numbers, the correct is "av=tomato + metescore;", i hope that help you.
 

Wing0

Estimable
Nov 5, 2014
47
0
4,590
1


Yes that works thank you.
 

Wing0

Estimable
Nov 5, 2014
47
0
4,590
1
I want to pause my program at the end too. system(pause) doesn't work, it tells me that pause is an identifier, and it is undefined.
 

Pinhedd

Distinguished
Moderator


If you wish to pause the program and wait for the user to press any key to terminate it, append something like the following:

C++:
fprintf(stdout,"Press any key to continue\n");
getchar();
return 0;
getchar is defined in stdio.h / cstdio

EDIT:

A word of advice moving forward. When writing functions, declare all local variables at the top of the function body regardless of whether or not you will use them.

For example, this is a bad programming style

C++:
int a;
fscanf(stdin,"%d",&a);
char *b = "This is a string";
int c;
c = foo(&a,b);
if (c)
{
double d = 1.0f;
a = bar(d);
}
else
{
a = 0;
}
This is a much better programming style

C++:
int a = 0, c = 0;
char *b = "This is a string";
double d = 1.0f;
fscanf(stdin,"%d",&a);
c = foo(&a,b);
if (c)
{
a = bar(d);
}
else
{
a = 0;
}
Space is allocated for local variables all at once when the function is called. It doesn't matter if the function requires 10 bytes of local storage, or 100 bytes of local storage, the time taken to allocate that memory is the same. If you collect all of the declarations at the top of the function body it will make your code much more readable.
 

Wing0

Estimable
Nov 5, 2014
47
0
4,590
1


Code:
#include "StdAfx.h"
#include <iostream>
#include <string>
#include "stdio.h"
using namespace std;

int main()
{
	string movie;
	cout << "What is the name of the movie? \n";
	getline(cin, movie);
	int avg;
	int metescore;
	int av;
	int tomato;
	cout << "what is the metascore for " << movie << "? \n";
		cin >> metescore;

		
		cout << "What is the tomatoscore for " << movie << "? \n";
		cin >> tomato;
		
		
			av = tomato + metescore;
		avg = av / 2;

			cout << "The average score for " << movie << " is " << avg << ".";
			 getchar();
			return 0;
}
Ok I added getchar, and all I get when I build is this:

========== Build: 0 succeeded, 0 failed, 1 up-to-date, 0 skipped ==========

Is that because I already built it?

And after I run the program, the debug window shows this:

'Program3.exe': Loaded 'C:\Users\Brady\Documents\Visual Studio 2010\Projects\Program3\Debug\Program3.exe', Symbols loaded.
'Program3.exe': Loaded 'C:\Windows\SysWOW64\ntdll.dll', Cannot find or open the PDB file
'Program3.exe': Loaded 'C:\Windows\SysWOW64\kernel32.dll', Cannot find or open the PDB file
'Program3.exe': Loaded 'C:\Windows\SysWOW64\KernelBase.dll', Cannot find or open the PDB file
'Program3.exe': Loaded 'C:\Windows\SysWOW64\msvcp100d.dll', Symbols loaded.
'Program3.exe': Loaded 'C:\Windows\SysWOW64\msvcr100d.dll', Symbols loaded.
The program '[3816] Program3.exe: Native' has exited with code 0 (0x0).



And it doesn't pause. I'd assume this means I don't have the library, if so, where can I get it?

And why do some libraries use quotes, and some use arrows?
iostream is #include <iostream>
and StdAfx is #include "StdAfx"

What's the difference?
 
Thread starter Similar threads Forum Replies Date
A Apps General Discussion 1
O Apps General Discussion 6
A Apps General Discussion 2
Z Apps General Discussion 8
D Apps General Discussion 5
L Apps General Discussion 1
B Apps General Discussion 3
A Apps General Discussion 2
A Apps General Discussion 13
B Apps General Discussion 2
A Apps General Discussion 5
D Apps General Discussion 1
S Apps General Discussion 2
V Apps General Discussion 1
S Apps General Discussion 4
L Apps General Discussion 8
A Apps General Discussion 1
G Apps General Discussion 3
H Apps General Discussion 10
S Apps General Discussion 1

ASK THE COMMUNITY