• 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.50/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;
}
 
Joined
Feb 26, 2008
Messages
4,876 (0.83/day)
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
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.04/day)
Location
Athens in love with Anna :)
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
828 (0.19/day)
Location
NL
System Name SIGSEGV
Processor INTEL i7-7700K | AMD Ryzen 2700X
Motherboard QUANTA | ASUS Crosshair VII Hero
Cooling Air cooling 4 heatpipes | Corsair H115i | Noctua NF-A14 IndustrialPPC Fan 3000RPM
Memory Micron 16 Gb DDR4 2400 | GSkill Ripjaws 32Gb DDR4 3200 3400(OC) 14-14-14-34 @1.38v
Video Card(s) Nvidia 1060 6GB | Gigabyte 1080Ti Aorus
Storage 1TB 7200/256 SSD PCIE | ~ TB | 970 Evo
Display(s) 15,5" / 27"
Case Black & Grey | Phanteks P400S
Audio Device(s) Realtek
Power Supply Li Battery | Seasonic Focus Gold 750W
Mouse g402
Keyboard Leopold|Ducky
Software LinuxMint KDE |UBUNTU | Windows 10 PRO
Benchmark Scores i dont care about scores
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.04/day)
Location
Athens in love with Anna :)
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,894 (0.50/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
OH...So I just needed to declare char for my variables.

Thanks.
 
Joined
Dec 2, 2009
Messages
3,351 (0.64/day)
System Name Dark Stealth
Processor Ryzen 5 5600x
Motherboard Gigabyte B450M Gaming rev 1.0
Cooling Snowman, arctic p12 x2 fans
Memory 16x2 DDR4 Corsair Dominator Pro
Video Card(s) 3080 10gb
Storage 2TB NVME PCIE 4.0 Crucial P3 Plus, 1TB Crucial MX500 SSD, 4TB WD RED HDD
Display(s) HP Omen 34c (34" monitor 3440x1440 165Hz VA panel)
Case Zalman S2
Power Supply Corsair 750TX
Mouse Logitech pro superlight, mx mouse s3, Razer Basiliskx with battery
Keyboard Custom mechanical keyboard tm680
Software Windows 11
Benchmark Scores 70-80 fps 3440x1440 on cyberpunk 2077 max settings
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.04/day)
Location
Athens in love with Anna :)
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,351 (0.64/day)
System Name Dark Stealth
Processor Ryzen 5 5600x
Motherboard Gigabyte B450M Gaming rev 1.0
Cooling Snowman, arctic p12 x2 fans
Memory 16x2 DDR4 Corsair Dominator Pro
Video Card(s) 3080 10gb
Storage 2TB NVME PCIE 4.0 Crucial P3 Plus, 1TB Crucial MX500 SSD, 4TB WD RED HDD
Display(s) HP Omen 34c (34" monitor 3440x1440 165Hz VA panel)
Case Zalman S2
Power Supply Corsair 750TX
Mouse Logitech pro superlight, mx mouse s3, Razer Basiliskx with battery
Keyboard Custom mechanical keyboard tm680
Software Windows 11
Benchmark Scores 70-80 fps 3440x1440 on cyberpunk 2077 max settings
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,783 (1.74/day)
Location
Stuck in a PC. halp.
System Name Monke | Work Thinkpad| Old Monke
Processor Ryzen 5600X | Ryzen 5500U | FX8320
Motherboard ASRock B550 Extreme4 | ? | Asrock 990FX Extreme 4
Cooling 240mm Rad | Not needed | hyper 212 EVO
Memory 2x16GB DDR4 3600 Corsair RGB | 16 GB DDR4 3600 | 16GB DDR3 1600
Video Card(s) Sapphire Pulse RX6700XT 12GB | Vega 8 | Sapphire Pulse RX580 8GB
Storage Samsung 980 nvme (Primary) | some samsung SSD
Display(s) Dell 2723DS | Some 14" 1080p 98%sRGB IPS | Dell 2240L
Case Ant Esports Tempered case | Thinkpad | Antec
Audio Device(s) Logitech Z333 | Jabra corpo stuff
Power Supply Corsair RM750e | not needed | Corsair GS 600
Mouse Logitech G400 | nipple
Keyboard Logitech G213 | stock kb is awesome | Logitech K230
VR HMD ;_;
Software Windows 10 Professional x3
Benchmark Scores There are no marks on my bench
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,783 (1.74/day)
Location
Stuck in a PC. halp.
System Name Monke | Work Thinkpad| Old Monke
Processor Ryzen 5600X | Ryzen 5500U | FX8320
Motherboard ASRock B550 Extreme4 | ? | Asrock 990FX Extreme 4
Cooling 240mm Rad | Not needed | hyper 212 EVO
Memory 2x16GB DDR4 3600 Corsair RGB | 16 GB DDR4 3600 | 16GB DDR3 1600
Video Card(s) Sapphire Pulse RX6700XT 12GB | Vega 8 | Sapphire Pulse RX580 8GB
Storage Samsung 980 nvme (Primary) | some samsung SSD
Display(s) Dell 2723DS | Some 14" 1080p 98%sRGB IPS | Dell 2240L
Case Ant Esports Tempered case | Thinkpad | Antec
Audio Device(s) Logitech Z333 | Jabra corpo stuff
Power Supply Corsair RM750e | not needed | Corsair GS 600
Mouse Logitech G400 | nipple
Keyboard Logitech G213 | stock kb is awesome | Logitech K230
VR HMD ;_;
Software Windows 10 Professional x3
Benchmark Scores There are no marks on my bench
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,586 (0.34/day)
Location
Kaunas, Lithuania
System Name my box
Processor AMD Ryzen 9 5950X
Motherboard ASRock Taichi x470 Ultimate
Cooling NZXT Kraken x72
Memory 2×16GiB @ 3200MHz, some Corsair RGB led meme crap
Video Card(s) AMD [ASUS ROG STRIX] Radeon RX Vega64 [OC Edition]
Storage Samsung 970 Pro && 2× Seagate IronWolf Pro 4TB in Raid 1
Display(s) Asus VG278H + Asus VH226H
Case Fractal Design Define R6 Black TG
Audio Device(s) Using optical S/PDIF output lol
Power Supply Corsair AX1200i
Mouse Razer Naga Epic
Keyboard Keychron Q1
Software Funtoo Linux
Benchmark Scores 217634.24 BogoMIPS
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.04/day)
Location
Athens in love with Anna :)
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: 532
  • Visual Basic.png
    Visual Basic.png
    7.6 KB · Views: 299
Joined
Dec 2, 2009
Messages
3,351 (0.64/day)
System Name Dark Stealth
Processor Ryzen 5 5600x
Motherboard Gigabyte B450M Gaming rev 1.0
Cooling Snowman, arctic p12 x2 fans
Memory 16x2 DDR4 Corsair Dominator Pro
Video Card(s) 3080 10gb
Storage 2TB NVME PCIE 4.0 Crucial P3 Plus, 1TB Crucial MX500 SSD, 4TB WD RED HDD
Display(s) HP Omen 34c (34" monitor 3440x1440 165Hz VA panel)
Case Zalman S2
Power Supply Corsair 750TX
Mouse Logitech pro superlight, mx mouse s3, Razer Basiliskx with battery
Keyboard Custom mechanical keyboard tm680
Software Windows 11
Benchmark Scores 70-80 fps 3440x1440 on cyberpunk 2077 max settings
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
 
Top