• We've upgraded our forums. Please post any issues/requests in this thread.

c++ if else help

Joined
Jul 18, 2008
Messages
2,866 (0.83/day)
Likes
697
Location
South Carolina
System Name KILLER
Processor Intel Core i7 4770K
Motherboard MSI Z87 G45 Gaming
Cooling Water Cooling
Memory 2x4GB Gskill 2400mhz
Video Card(s) EVGA GTX 780TI
Storage SSD Kingston 240GB
Display(s) 24" Asus 144hz
Case Cooler Master HAF
Audio Device(s) X-Fi Platinum
Power Supply Antec 1200 Watt Modular
Software Windows 7 64 Bit
Benchmark Scores 3DMark Vantage 51342
#1
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;
}
 
Joined
Feb 26, 2008
Messages
4,870 (1.36/day)
Likes
681
Location
Joplin, Mo
System Name Ultrabeast GX2
Processor Intel Core 2 Duo E8500 @ 4.0GHZ 24/7
Motherboard Gigabit P35-DS3L
Cooling Rosewill RX24, Dual Slot Vid, Fan control
Memory 2x1gb 1066mhz@850MHZ DDR2
Video Card(s) 9800GX2 @ 690/1040
Storage 750/250/250/200 all WD 7200
Display(s) 24" DCLCD 2ms 1200p
Case Apevia
Audio Device(s) 7.1 Digital on-board, 5.1 digital hooked up
Power Supply 700W RAIDMAXXX SLI
Software winXP Pro
Benchmark Scores 17749 3DM06
#2
It seems from the code that it does. Care to explain? Like are you wanting the screen to be cleared after each question?
 

MrSeanKon

New Member
Joined
Nov 14, 2006
Messages
267 (0.07/day)
Likes
73
Location
Athens in love with Anna :)
#3
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!");
}
 
Joined
Mar 31, 2012
Messages
542 (0.26/day)
Likes
113
System Name SIGSEGV
Processor Intel | AMD
Motherboard INVALID| X399
Cooling 2 Heatpipes | EK
Memory 16 Gb DDR4 2400 | 32 GB DDR4
Video Card(s) 1060 | MI25 / Vega Frontier | Tesla K
Storage 1TB 7200/256 SSD PCIE | ~ TB
Display(s) 15,5" / 27" LEDTV | No Display
Case Black & Grey
Audio Device(s) Realtek
Power Supply Silverstone 850W | Unrated PSU
Software LinuxMint KDE | XFCE
Benchmark Scores i dont care about scores
#4
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:

MrSeanKon

New Member
Joined
Nov 14, 2006
Messages
267 (0.07/day)
Likes
73
Location
Athens in love with Anna :)
#5
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:
Joined
Jul 18, 2008
Messages
2,866 (0.83/day)
Likes
697
Location
South Carolina
System Name KILLER
Processor Intel Core i7 4770K
Motherboard MSI Z87 G45 Gaming
Cooling Water Cooling
Memory 2x4GB Gskill 2400mhz
Video Card(s) EVGA GTX 780TI
Storage SSD Kingston 240GB
Display(s) 24" Asus 144hz
Case Cooler Master HAF
Audio Device(s) X-Fi Platinum
Power Supply Antec 1200 Watt Modular
Software Windows 7 64 Bit
Benchmark Scores 3DMark Vantage 51342
#6
OH...So I just needed to declare char for my variables.

Thanks.
 
Joined
Dec 2, 2009
Messages
3,257 (1.11/day)
Likes
315
System Name Mercury KM-81
Processor Phenom II x4 B50
Motherboard ASUS M4A89GTD PRO
Cooling Cooler Master Hyper TX3
Memory 6GB Ram (4GBx1 Corsair 2GBx1 Nanya)
Video Card(s) PowerColor AX6770 V2.0
Storage 2TB WD Black, 1TB Hitachi, 500 GB WD Blue
Display(s) Philips 247E-LPH 24" 1920x1080
Case Mercury
Audio Device(s) Integrated Realtek
Power Supply Corsair 750TX
Software Windows 7 64-bit
Benchmark Scores Maybe it is time to benchmark :D
#7
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.
 

MrSeanKon

New Member
Joined
Nov 14, 2006
Messages
267 (0.07/day)
Likes
73
Location
Athens in love with Anna :)
#8
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:
Joined
Dec 2, 2009
Messages
3,257 (1.11/day)
Likes
315
System Name Mercury KM-81
Processor Phenom II x4 B50
Motherboard ASUS M4A89GTD PRO
Cooling Cooler Master Hyper TX3
Memory 6GB Ram (4GBx1 Corsair 2GBx1 Nanya)
Video Card(s) PowerColor AX6770 V2.0
Storage 2TB WD Black, 1TB Hitachi, 500 GB WD Blue
Display(s) Philips 247E-LPH 24" 1920x1080
Case Mercury
Audio Device(s) Integrated Realtek
Power Supply Corsair 750TX
Software Windows 7 64-bit
Benchmark Scores Maybe it is time to benchmark :D
#9
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?
 

de.das.dude

Pro Indian Modder
Joined
Jun 13, 2010
Messages
8,202 (2.99/day)
Likes
2,390
Location
Pune
System Name la de da... | Samsung NP355V5C-S05IN
Processor FX 8320 | AMD A8-4500
Motherboard ASRock 990FX Extreme 4 | Samsung something something
Cooling CM Hyper 212 EVO push:pull+ 1 panaflow 113CFM + 2 x 120mm NZXTs | Coolermaster Notepal
Memory 2x4GB DDR3 Corsair Vengeance 1600MHz CL9 | Samsunng 4GB + 2GB DDR3 1600MHz
Video Card(s) AMD 7790 GHZ edition! | 7670M + 7640G
Storage 1TB WD10EZEX | W.D 500GB Green AADS | Samsung 1TB
Display(s) dell S2240l 1080p IPS 21.5" | Samsung antiglare 15.6"
Case NZXT Guardian 921RB(@home) and Antec ASK4000B U3(Current)
Audio Device(s) Realtek ALC892 with THX True Studio | Realtek something
Power Supply Corsair GS600 | 90W Brick, 6 cell 48000mAh battery
Mouse Logitech G400, GOD TIER MOUSE!! LOGitech F310 gamepad!
Keyboard Logitech K200
Software Windows 8.1 x64 | Windows 8.1 x64
Benchmark Scores real men do bench presses not bench marks.
#10
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++
 

de.das.dude

Pro Indian Modder
Joined
Jun 13, 2010
Messages
8,202 (2.99/day)
Likes
2,390
Location
Pune
System Name la de da... | Samsung NP355V5C-S05IN
Processor FX 8320 | AMD A8-4500
Motherboard ASRock 990FX Extreme 4 | Samsung something something
Cooling CM Hyper 212 EVO push:pull+ 1 panaflow 113CFM + 2 x 120mm NZXTs | Coolermaster Notepal
Memory 2x4GB DDR3 Corsair Vengeance 1600MHz CL9 | Samsunng 4GB + 2GB DDR3 1600MHz
Video Card(s) AMD 7790 GHZ edition! | 7670M + 7640G
Storage 1TB WD10EZEX | W.D 500GB Green AADS | Samsung 1TB
Display(s) dell S2240l 1080p IPS 21.5" | Samsung antiglare 15.6"
Case NZXT Guardian 921RB(@home) and Antec ASK4000B U3(Current)
Audio Device(s) Realtek ALC892 with THX True Studio | Realtek something
Power Supply Corsair GS600 | 90W Brick, 6 cell 48000mAh battery
Mouse Logitech G400, GOD TIER MOUSE!! LOGitech F310 gamepad!
Keyboard Logitech K200
Software Windows 8.1 x64 | Windows 8.1 x64
Benchmark Scores real men do bench presses not bench marks.
#11
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.
 
Joined
Jul 23, 2011
Messages
1,555 (0.66/day)
Likes
1,459
Location
Kaunas, Lithuania
System Name Eternal WIP ][ HP ProLiant server blade
Processor AMD FX-8320 @ 4.4/4.1 GHz ][ 2x Intel Xeon L5520 @ 2.27GHz
Motherboard Asus Crosshair V Formula-Z ][ -
Cooling Corsair H55 ][ Stock
Memory 2x4GB Kingston HyperX Genesis @ 1866MHz ][ 4x4 GiB Hynix @ 1333 MHz; ECC on
Video Card(s) 2x MSI GeForce GTX 770 DirectCU II OC ][ None
Storage 1TB WD Black + 1.5TB WD Green + 750GB Samsung + 64GB OCZ Agility 4 ][ 3x 1 TB Hitachi
Display(s) Asus VG278H + Asus VH226H ][ None
Case Antec Nine Hundred ][ it's a server blade, lol
Audio Device(s) Supreme FX II (Integrated); Using optical S/PDIF output ][ none
Power Supply Corsair AX1200i ][ [stock]
Software Gentoo Linux ][ Linux Mint 16
Benchmark Scores 65970 BogoMIPS. Ha. Ha. Ha. >=|
#12
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"
 

MrSeanKon

New Member
Joined
Nov 14, 2006
Messages
267 (0.07/day)
Likes
73
Location
Athens in love with Anna :)
#13
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

Joined
Dec 2, 2009
Messages
3,257 (1.11/day)
Likes
315
System Name Mercury KM-81
Processor Phenom II x4 B50
Motherboard ASUS M4A89GTD PRO
Cooling Cooler Master Hyper TX3
Memory 6GB Ram (4GBx1 Corsair 2GBx1 Nanya)
Video Card(s) PowerColor AX6770 V2.0
Storage 2TB WD Black, 1TB Hitachi, 500 GB WD Blue
Display(s) Philips 247E-LPH 24" 1920x1080
Case Mercury
Audio Device(s) Integrated Realtek
Power Supply Corsair 750TX
Software Windows 7 64-bit
Benchmark Scores Maybe it is time to benchmark :D
#14
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