• Welcome to TechPowerUp Forums, Guest! Please check out our forum guidelines for info related to our community.

c++ if else help

Joined
Jul 18, 2008
Messages
2,894 (0.47/day)
Location
South Carolina
System Name KILLER
Processor Intel Core i7 4790K
Motherboard MSI Z87 G45 Gaming
Cooling Water Cooling
Memory 2x8GB Gskill 2400mhz
Video Card(s) EVGA GTX 980
Storage SSD Samsung 500GB
Display(s) 24" Asus 144hz
Case Cooler Master HAF
Audio Device(s) X-Fi Platinum
Power Supply Antec 1200 Watt Modular
Software Windows 10 64 Bit
Benchmark Scores 3DMark Vantage 51342
I need the program to ask each question seperately.
What do I need to add to my code?
Please help...Thanks
Code:
//Your Name Here
#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
	//Sample variable data types
	int test = 0;
	int clas = 0;
	int thanksgiving = 0;
	

//Place your code here...


	cout<<"\t\t\t ******************************"<<endl;
	cout<<"\t\t\t *     Kelvin Holland         *"<<endl;
	cout<<"\t\t\t *        CPT-168             *"<<endl;
	cout<<"\t\t\t *         Exam               *"<<endl;
	cout<<"\t\t\t ******************************"<<endl<<endl;

	cout<<"Did you study for this test?";
	cin>>test;

	if(test == 'y')
	{
	cout<<"Good for you"<<endl<<endl;
	}
	
	else
	{
	cout<<"Good luck then"<<endl<<endl;
	}
	

	cout<<"Have you enjoyed this class?"<<endl<<endl;
	cin>>clas;

	if(clas == 'y')
	{
	cout<<"That is nice"<<endl<<endl;
	}
	
	else
	{
	cout<<"You should have answered Yes"<<endl<<endl;
	}

	cout<<"Did you have a good Thanksgiving?"<<endl<<endl;
	cin>>thanksgiving;

	if(thanksgiving == 'y')
	{
	cout<<"Great!"<<endl<<endl;
	}
	
	else
	{
	cout<<"Great!"<<endl<<endl;
	}





	
      


//Sample Screen Output
	cout<<"\t\t Have a Merry Christmas and a Happy New Year!!!"<<endl<<endl;


	
	system("color F0");
	system("pause");
	return 0;
}
 
It seems from the code that it does. Care to explain? Like are you wanting the screen to be cleared after each question?
 
I cannot understand why you declared three integer variables but they store characters.... :rolleyes:
One the other hand cout, cin etc......
They suck! :p
Use printf, getchar instead ;)
Anyway I modded your code and runs OK under Visual C++ 2010. Do the rest moddifications.

Code:
#include <iostream>
#include <iomanip>
using namespace std;

void main(void)
{
	char test,classroom,thanksgiving;   
	
	printf("\t\t\t ******************************\n");          
	printf("\t\t\t *     Kelvin Holland         *\n");
	printf("\t\t\t *        CPT-168             *\n");
	printf("\t\t\t *         Exam                *\n");
	printf("\t\t\t ******************************\n\n\n");

	cout<<"Did you study for this test? ";
	cin>>test;
	if (test == 'y')   // All variables must be characters!!!!
		cout<<"Good for you"<<endl<<endl;
	else
		cout<<"Good luck then"<<endl<<endl;
	
	cout<<"Have you enjoyed this class? ";
	cin>>classroom;
	if (classroom == 'y')
		cout<<"That is nice"<<endl<<endl;
	else
		cout<<"You should have answered Yes"<<endl<<endl;
	
	cout<<"Did you have a good Thanksgiving? ";
	cin>>thanksgiving;
	if (thanksgiving == 'y')
		cout<<"Great!"<<endl<<endl;
	else
		cout<<"Great!"<<endl<<endl;
	
	cout<<"\t\t        Anna loves me too much!!!"<<endl<<endl;
	system("color F0");
	system("pause");
}

Moreover no reason to use {, }.
You must use them in compound statements.
Example:

Code:
for (int i=1; i<=10; i++)
{
    k=i*i;
    if (k > 8)
      printf("Hello world");
    else
       printf("Damn!");
}
 
I cannot understand why you declared three integer variables but they store characters.... :rolleyes:
One the other hand cout, cin etc......
They suck! :p
Use printf, getchar instead ;)
Anyway I modded your code and runs OK under Visual C++ 2010. Do the rest moddifications.

you said that cout or/and cin is (are) sucks compared with printf, can you explain that? :)

Moreover no reason to use {, }.
You should use them in compound statements.

fixed
 
Last edited:
First of all, I am not a programmer. Basically I am a hobbyist.
I started programming in pure ANSI C (scanf, printf, getchar etc) 20 years ago reading C step by step by Waite Group and I did not program in C++.
That's why I am not familiiar with cin, cout etc and posted that.
Of course cout, cin do the same work. It is up to everyone which commands he/she prefers.
Todays I program in C#. I prefer this:

Code:
public class Example
{
   public static void Main()
   {
      printf("With the default new line characters:\n");
   }
}

instead of:

Code:
public class Example
{
   public static void Main()
   {
      System.Console.WriteLine("With the default new line characters:");
      Console.WriteLine();      
   }
}

Because C is a subset (which means that any C command is supported by C++, C#) I cannot understand why they developed equivalent (OK not at all) commands in them....
 
Last edited:
OH...So I just needed to declare char for my variables.

Thanks.
 
Replace the integer with char on your code and it will perfectly work:
char test;
char clas;
char thanksgiving;

As for the cout to be replaced with prinf, I was 'terribly' amazed.
Only thing i will tell you about the difference is a simple example
Code:
printf(a, %d); // dates in 1985 C style
cout << a; // dates in 1991 C++ style

cout is intelligent and it will find your strings, but printf, will not.
cout has a more powerful library with a lot of functions bundled which
you will find extremely important during your C++ journey

Also, remove that system('pause'); since it dates back in C journey
and it will consume so much computer power you can't even imagine.
Replace it with cin.get(); in all your projects.
If you have used cin before to get a variable, like in your example,
you will need to add 2x cin.get();
which means:
cin.get();
cin.get();
return 0;

In c++98, return 0 has been removed since the compiler will know when the main function will end.
So you can remove it and save time

I know one last method, which is the most important one, but i will not reveal at this stage where you are.
 
As for the cout to be replaced with prinf, I was 'terribly' amazed.
Our friend would to create a simple program and result matters instead of using ANSI C or C++ statements.
Of course scanf & printf can read and write strings. But it is a beyond of my scope to post more code about this => off topic.
 
Last edited:
One thing i was amazed even more, was that line of code:
#include <iomanip>

I am amazed by same programmers, even professionals of adding more includes than needed.
Can you explain pls why did you use that include at first? And where did you get that?
 
Use printf, getchar instead

umm... cout cin is best suited for beiginers and IT IS C++, not C :p

to be honest i never required the use of printf or getchar when doing C++
 
One thing i was amazed even more, was that line of code:
#include <iomanip>

I am amazed by same programmers, even professionals of adding more includes than needed.
Can you explain pls why did you use that include at first? And where did you get that?

lack of basic knowledge about what functions the library files actually contain.
 
As for the cout to be replaced with prinf, I was 'terribly' amazed.
Only thing i will tell you about the difference is a simple example
Code:
printf(a, %d); // dates in 1985 C style
cout << a; // dates in 1991 C++ style

cout is intelligent and it will find your strings, but printf, will not.
cout has a more powerful library with a lot of functions bundled which
you will find extremely important during your C++ journey.

There were things that I could not do with cout, but could easly do with printf. I suspect one of those things was not doable with cout was because cout was too intelligent to blindly do what asked to do.
Also, printf looks much better when one needs to print out a lot of stuff. Formatting complicated output with a lot of variables and stuff requires a sh*tload of "<<" operators, so it looks horrible and is hard to edit... "f*** it, I'll just use printf"
 
Thanks Vinska for your post. I would type something like that but I did not it (I explained above the reasons).
Asylum focus to the photos. Both are simple codes. But Visual Basic (as Pascal) protect you from run time bugs avoiding compilation. Simply, Visual Basic does not allow to compare different types.
Therefore be very careful with C++ freedom :)
 

Attachments

  • Visual C++.png
    Visual C++.png
    6.5 KB · Views: 563
  • Visual Basic.png
    Visual Basic.png
    7.6 KB · Views: 370
never mind guys, they both have their pros and cons, that is sure
i was amazed, since i read in Stephen Prata's book, to shift to cout
and i have been using cout for like 1.5 years
 
Back
Top