if statement issues

argument error and a diappearing command window, again

  • (2 Pages)
  • +
  • 1
  • 2

28 Replies - 794 Views - Last Post: 05 March 2010 - 09:28 PM Rate Topic: -----

#1 mnm2317  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 31-January 10

if statement issues

Posted 03 March 2010 - 11:11 PM

I'm doing some logic coding for class. Right now it's not the logic I'm having trouble with, it's getting no errors and getting the window to simply stop closing on me (again) that's my problem.

First off my 'if' statement gives me about 60 errors. I think that's got to do with the fact that I'm using "==" and "&&" inside of it. I never remember having those comparative issues like I'm having now, but is there some sort of "#include" file that I'm missing to make those work?
Also, the command window simply closes once the 'cout...A is lying' is printed. I used those 'cin's at the end to get it to stop that, but now that I've put more code it, they don't work. So those are my two issues/questions, and here's my code:

#include "stdafx.h";
#include <iostream>
#include <string>
using namespace std;

int main()
{
	int p, q, r,t,f;

	cout << "A: 'We are both telling the truth.'";
	cin >> p;
	cout << endl;
	cout << "B: 'A is lying.'";
	cin >> q;
	cout << endl;

	if (p==t && q==f){
		cout >> "This is consistent.";
	else
		cout >> "This is inconsisten.";
	}


//waits for user to press a key
	cin.ignore();
	cin.get();
}


here's the exact error (one of them):
Error 1 error C2784: 'std::basic_istream<_Elem,_Traits> &std::operator >>(std::basic_istream<_Elem,_Traits> &,std::basic_string<_Elem,_Traits,_Alloc> &)' : could not deduce template argument for 'std::basic_istream<_Elem,_Traits> &' from 'std::ostream' .... 19

Is This A Good Question/Topic? 0
  • +

Replies To: if statement issues

#2 Martyn.Rae  Icon User is offline

  • The programming dinosaur
  • member icon

Reputation: 538
  • View blog
  • Posts: 1,406
  • Joined: 22-August 09

Re: if statement issues

Posted 03 March 2010 - 11:21 PM

This code fixes the errors:

#include <iostream> 
#include <string> 
using namespace std; 
 
int main() 
{ 
        int p, q, r,t,f; 
 
        cout << "A: \'We are both telling the truth.\'"; 
        cin >> p; 
        cout << endl; 
        cout << "B: \'A is lying.\'"; 
        cin >> q; 
        cout << endl; 
 
        if (p==t && q==f) { 
                cout << "This is consistent.";
        }
        else {
                cout << "This is inconsisten."; 
        } 
 
 
//waits for user to press a key 
        cin.ignore(); 
        cin.get(); 
}



You had used the >> operator on cout and i should be <<. You had missing braces on the if and else keywords. You also had not used the \ before the single quotes in your literals. You should have had \' like I have.

As for the rest of it ... well it does not make sense. You will get a runtime failure as the variables t and f are not initialised, and the variable r is not used at all.

This post has been edited by Martyn.Rae: 03 March 2010 - 11:22 PM

Was This Post Helpful? 1
  • +
  • -

#3 Israel  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 4
  • View blog
  • Posts: 788
  • Joined: 22-November 04

Re: if statement issues

Posted 03 March 2010 - 11:24 PM

Instead of this:
        if (p==t && q==f){
                cout >> "This is consistent.";
        else
                cout >> "This is inconsisten.";
        }



Try this:
        if (p==t && q==f){
                cout >> "This is consistent.";
        }
        else{
                cout >> "This is inconsisten.";
        }



See if that does get ride of a couple for starters.

Quote

but is there some sort of "#include" file that I'm missing to make those work?


Well, if #include "stdafx.h" is not in the same directory as your C/C++ file, then YES!
Was This Post Helpful? 1
  • +
  • -

#4 PlasticineGuy  Icon User is offline

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

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

Re: if statement issues

Posted 03 March 2010 - 11:30 PM

Quote

Well, if #include "stdafx.h" is not in the same directory as your C/C++ file, then YES!
Only if you are using precompiled headers (which I think is a pain in the arse and always disable).
Was This Post Helpful? 0
  • +
  • -

#5 Israel  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 4
  • View blog
  • Posts: 788
  • Joined: 22-November 04

Re: if statement issues

Posted 03 March 2010 - 11:43 PM

Only if you are using precompiled headers (which I think is a pain in the arse and always disable). 


It was my understanding if the .h file was in directory at compile time and was in " " instead of < > like normal then it would compile then. No need for pre-compiled headers
Was This Post Helpful? 0
  • +
  • -

#6 Israel  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 4
  • View blog
  • Posts: 788
  • Joined: 22-November 04

Re: if statement issues

Posted 03 March 2010 - 11:49 PM

Oh great you took away a reputation point from me for something you don't understand. Great. By the way, I believe you meant Pre-Processed, not Pre-Compiled!
Was This Post Helpful? 0
  • +
  • -

#7 PlasticineGuy  Icon User is offline

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

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

Re: if statement issues

Posted 03 March 2010 - 11:50 PM

stdafx is the precompiled header. I do not see the purpose of it and have never used it.
Was This Post Helpful? 0
  • +
  • -

#8 Martyn.Rae  Icon User is offline

  • The programming dinosaur
  • member icon

Reputation: 538
  • View blog
  • Posts: 1,406
  • Joined: 22-August 09

Re: if statement issues

Posted 03 March 2010 - 11:58 PM

View PostPlasticineGuy, on 04 March 2010 - 05:50 AM, said:

stdafx is the precompiled header. I do not see the purpose of it and have never used it.


Sorry, PlacticineGuy, but stdafx.h is not the precompiled header ... although you are not far off. The problem with precompiled headers is the fact that changing a header's position in the order of includes, adding or removing a header invalidates the precompiled header file. What Microsoft tried to do was to ensure that all include files were in once central place. Then if the timestamp changed on that header file, they knew that they could not rely on the precompiled header file.

I agree with you on the point that they are a pain. I also always switch them off, but today with machines as powerful as they are, I do not think that precompiled headers are useful. Long gone are the days when compilations took an hour to complete (programs with around 20,000 lines of user code).

PS I up-ed both your votes cancelling out the -1. Both of you consider yourselves slapped on the wrist!

This post has been edited by Martyn.Rae: 04 March 2010 - 12:00 AM

Was This Post Helpful? 1
  • +
  • -

#9 no2pencil  Icon User is offline

  • Original Digital Gansta
  • member icon

Reputation: 4466
  • View blog
  • Posts: 24,916
  • Joined: 10-May 07

Re: if statement issues

Posted 04 March 2010 - 12:02 AM

View PostMartyn.Rae, on 04 March 2010 - 12:58 AM, said:

Both of you consider yourselves slapped on the wrist!

or in the face...
Was This Post Helpful? 0
  • +
  • -

#10 Martyn.Rae  Icon User is offline

  • The programming dinosaur
  • member icon

Reputation: 538
  • View blog
  • Posts: 1,406
  • Joined: 22-August 09

Re: if statement issues

Posted 04 March 2010 - 12:04 AM

View Postno2pencil, on 04 March 2010 - 06:02 AM, said:

View PostMartyn.Rae, on 04 March 2010 - 12:58 AM, said:

Both of you consider yourselves slapped on the wrist!

or in the face...


:gun_bandana: going a bit too far perhaps?
Was This Post Helpful? 1
  • +
  • -

#11 no2pencil  Icon User is offline

  • Original Digital Gansta
  • member icon

Reputation: 4466
  • View blog
  • Posts: 24,916
  • Joined: 10-May 07

Re: if statement issues

Posted 04 March 2010 - 12:06 AM

:detective: :gunsmilie:
Was This Post Helpful? 0
  • +
  • -

#12 PlasticineGuy  Icon User is offline

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

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

Re: if statement issues

Posted 04 March 2010 - 12:18 AM

Also, @OP: It is good practice not to omit the return statement.

EDIT: I thought it was a precompiled header because VC++ complains if I don't include stdafx UNLESS I set the Create/Use Precompiled Header to Not Using Precompiled Header.

This post has been edited by PlasticineGuy: 04 March 2010 - 12:30 AM

Was This Post Helpful? 1
  • +
  • -

#13 mnm2317  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 31-January 10

Re: if statement issues

Posted 05 March 2010 - 10:54 AM

View PostMartyn.Rae, on 03 March 2010 - 10:21 PM, said:

This code fixes the errors:

#include <iostream> 
#include <string> 
using namespace std; 
 
int main() 
{ 
        int p, q, r,t,f; 
 
        cout << "A: \'We are both telling the truth.\'"; 
        cin >> p; 
        cout << endl; 
        cout << "B: \'A is lying.\'"; 
        cin >> q; 
        cout << endl; 
 
        if (p==t && q==f) { 
                cout << "This is consistent.";
        }
        else {
                cout << "This is inconsisten."; 
        } 
 
 
//waits for user to press a key 
        cin.ignore(); 
        cin.get(); 
}



You had used the >> operator on cout and i should be <<. You had missing braces on the if and else keywords. You also had not used the \ before the single quotes in your literals. You should have had \' like I have.

As for the rest of it ... well it does not make sense. You will get a runtime failure as the variables t and f are not initialised, and the variable r is not used at all.


Yea, I totally see what you mean now. Those 't' and 'f's should not have been variables in the first place. However now I have a question about that. I changed the if statement to this:
if (p==1 && q==2){
		cout << "This is consistent.";
	}
	else{
		cout << "This is inconsistent.";
	}


I changed the 't' to a '1' and 'f' to a '2' and checked the logic, which works out. However I need the code to check for a 'T' and 'F' since that's what I'll be asking the user to input. So how would I do those? Something like "p==char T" or something?
Was This Post Helpful? 0
  • +
  • -

#14 sarmanu  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 965
  • View blog
  • Posts: 2,362
  • Joined: 04-December 09

Re: if statement issues

Posted 05 March 2010 - 10:58 AM

You want to check if your input if T or F?
Was This Post Helpful? 0
  • +
  • -

#15 mnm2317  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 31-January 10

Re: if statement issues

Posted 05 March 2010 - 07:46 PM

Yes, but I did figure it out, here it is:
if( (p=='f') || (p=='F') && (q=='t') || (q=='T') ){
		cout << "This is consistent.";
	}
	else{
		cout << "This is inconsistent.";
	}



Thx for all the help everyone!

This post has been edited by mnm2317: 05 March 2010 - 08:24 PM

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2