How to start a newline in C++ while doing equations - c++

Im reading through "The C++ Programming Language" and my current assignment is to make a program that takes two variables and determines the smallest, largest, sum, difference, product, and ratio of the values.
Problem is i can't start a newline. "\n" doesn't work because i have variables after the quote. And "<< endl <<" only works for the first line. I googled the hell out of this problem and im coming up short.
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>
using namespace std;
inline void keep_window_open() {char ch;cin>>ch;}
int main()
{
int a;
int b;
cout<<"Enter value one\n";
cin>>a;
cout<<"Enter value two\n";
cin>>b;
(a>b); cout<< a << " Is greater than " << b;
(a<b); cout<< a << " Is less than " << b;
keep_window_open();
return 0;
}

You are looking for std::endl, but your code won't work as you expect.
(a>b); cout<< a << " Is greater than " << b;
(a<b); cout<< a << " Is less than " << b;
This is not a condition, you need to rewrite it in terms of
if(a>b) cout<< a << " Is greater than " << b << endl;
if(a<b) cout<< a << " Is less than " << b << endl;
You can also send the character \n to create a new line, I used endl as I thought that's what you were looking for. See this thread on what could be issues with endl.
The alternative is written as
if(a>b) cout<< a << " Is greater than " << b << "\n";
if(a<b) cout<< a << " Is less than " << b << "\n";
There are a few "special characters" like that, \n being new line, \r being carriage return, \t being tab, etc... useful stuff to know if you're starting.

You can output std::endl to the stream to move to the next line, like this:
cout<< a << " Is greater than " << b << endl;

Related

compiling is okay but my program doesn't really work

#include <iostream>
#include <string.h>
using namespace std;
int main()
{
while (1)
{
char name1[100];
char adrs1[100];
char rsn1[100];
char XXXXX[100];
cout << "input personal information" << '\n';
cout << "patient 1" << '\n';
cout << "input the name of the patient" << '\n';
cin.getline (name1,100);
cout << "input the address of the patient" << '\n';
cin.getline (adrs1,100);
cout << "input the reason" << '\n';
cin.getline (rsn1,100);
cout << "input the name of the patient" << '\n';
cout << "if you want to exit, input exit" << '\n';
cin.getline (XXXXX,100);
if (XXXXX==name1)
cout << adrs1[100] << rsn1[100] << '\n';
else (XXXXX=="exit");
break;
return 0;
}
}
that's my program, and compiling is okay. but when i start the program, it doesn't print any rsn or adrs, it just ends.
I want it to print rsn and adrs when it reads names.
Help me please
There are quite a few errors in your program.
The most important one is that you are trying to write an infinite loop. But it runs exactly once. You need to move your return statement out of the loop.
There is no need for a conditional statement for an else block. You can remove it along with the semi colon.
You're trying to print a character at the index 100 which goes out of bounds.
I don't know what XXXXX is supposed to be. May be you missed pasting the declaration on this website.
At this point, I really suggest picking up a book or trying to debug your code by going step-by-step through your code. It would be more helpful to you at this stage in your learning than this website,
To complete the answer above, the correct program would be:
#include <iostream>
#include <string.h>
using namespace std;
int main()
{
while (1)
{
char name1[100];
char adrs1[100];
char rsn1[100];
char XXXXX[100];
cout << "input personal information" << '\n';
cout << "patient 1" << '\n';
cout << "input the name of the patient" << '\n';
cin.getline (name1,100);
cout << "input the address of the patient" << '\n';
cin.getline (adrs1,100);
cout << "input the reason" << '\n';
cin.getline (rsn1,100);
cout << "input the name of the patient" << '\n';
cout << "if you want to exit, input exit" << '\n';
cin.getline (XXXXX,100);
if (strcmp(XXXXX,name1) == 0)
cout << adrs1 << rsn1 << '\n';
else /*(XXXXX=="exit");*/
break;
//return 0;
}
}
You forgot to initilize the name1 variable, you can initialize it using char name1[100] = {};
You cannot directly compare the if (XXXXX==name1), use can use the strncmp function for the same. I will prefer the string class instead of char pointer. Use the following:
if (!strncmp(XXXXX,name1,100))
cout << adrs1 << rsn1 << '\n';
else if (!strncmp(XXXXX,"exit",100))
break;

Visual Studio Appears to be ignoring my IF function

I've been trying to practice small parts of my coding for an impending assignment but for the life of me I can't get visual studio to recognise that there's an if function, it just completely skips over it. The window closes as soon as I press enter.
#include <iostream>
using namespace std;
int main()
{
int a, b;
cout << "Enter first number.";
cin >> a;
cout << "Enter second number.";
cin >> b;
if( a > b)
{
cout << "Variable a is greater than variable b." << endl;
cout << "Value of a is " << a << " value of b is " << b << endl;
}
return 0;
}
Not portable but, as you're using Visual Studio, you're on Windows, so it'll work. When you works in Debug mode, with Visual Studio, you don't need to add system("pause"), or something else, to your program. So I guess you're working in Release mode.
#include <cstdlib>
#include <iostream>
using namespace std;
int main()
{
int a, b;
cout << "Enter first number.";
cin >> a;
cout << "Enter second number.";
cin >> b;
if( a > b)
{
cout << "Variable a is greater than variable b." << endl;
cout << "Value of a is " << a << " value of b is " << b << endl;
}
system("pause");
return 0;
}
EDIT :
You shouldn't use std::endl for a new line, or at least not each time you need a new line. You should just write << "\n"; std::endl insert a new line character + flushes the buffer. That's important to understand the difference between << std::endl; and << "\n";

I'm new to C++ and I have few questions I'd like to ask

Question is :
Please enter the number of gallons of gasoline: 100
Original number of gallons is: 100
100 gallons is the equivalent of 378.54 liters
Thanks for playing
how to display "Original number of gallons is: 100"
and my "Thanks for playing" is combined with "press any key to continue" I dont know how to seperate them.
#include <iostream>
using namespace std;
int main()
{
float g;
float l;
cout << "Please enter the number of gallons of gasoline ";
cin >> g;
l = g*3.7854;
cout << g << " Gallon is the equivalent of " << l << "liters" << endl;
cout << "Thank you for playing";
system("pause");
return 0;
}
Do not use "use namespace std;", this is bad practice.
Using system("pause") is also bad practice.
Use
std::cout << std::endl;
To output a newline, to separate different lines of text.
#include <conio.h> // Added for _getch();
#include <iostream>
// removed "using namespace std;"
int main() {
float g = 0; // Added Initialization To The Variables
float l = 0;
// From Here On Out I Will Be Using The Scope Resolution Operator To Access the std namespace.
std::cout << "Please enter the number of gallons of gasoline" << std::endl;
std::cin >> g;
l = g*3.7854f; // Added an 'f' at the end since you declared floats and not doubles
std::cout "Original number of gallon(s) is: " << g << std::endl;
std::cout << g << "Gallon(s) is the equivalent of " << l << "liter(s)" << std::endl;
std::cout << "Thank you for playing" << std::endl;
// Changed System( "pause" ); To
std::cout << "\nPress any key to quit!\n";
_getch();
return 0;
} // main
Try placing the variables outside the main.
Also, get rid of the system("pause"); and at the end of every cout, before the semicolon, add << "\n";
At the end, the code should look like this:
#include <iostream>
using namespace std;
float g;
float l;
int main()
{
cout << "Please enter the number of gallons of gasoline ";
cin >> g;
cout << "Original number of gallons is: " << g << "\n";
l = g*3.7854;
cout << g << " gallon(s) is/are the equivalent of " << l << " liters\n";
cout << "Thank you for playing\n";
return 0;
}

Printing multiple outputs on same line

there. I'm self learning C++ out of "C++ without fear". There is an exercise dealing with the GCD of 2 numbers that asks to print "GCD(a,b) =>" at each step in the proceedure. I was able to get this working:
int gcd (int a, int b);
int main() {
int i,j;
cout << "Enter the first integer" << endl;
cin >> i;
cout << "Enter the second integer" << endl;
cin >> j;
int k = gcd(i,j);
cout << "The GCD is " << k << endl;
system("PAUSE");
return 0;
}
int gcd (int a, int b){
if(b==0){
cout << "GCF(" << a;
cout << "," << b;
cout << ") => " <<endl;
return a;
}
else {
cout << "GCF(" << a;
cout << "," << b;
cout << ") => " << endl;
return gcd(b,a%b);
}
}
I was just wondering if there is a nicer way to go about printing each step of finding the GCD. That is, is there a "nicer" way to write this part of the code:
cout << "GCF(" << a;
cout << "," << b;
cout << ") => " << endl;
? Thanks in advance.
You can do something like:
cout << "GCF(" << a << ',' << b << ") =>" << endl;
It is not C++ but you could use the C way of printing it which in my opinion looks better in this situation because there are far fewer stream operators, << in the way.
#include <cstdio>
printf("GCF(%d, %d) =>\n", a, b);
But this is a C way of doing things... You could use something like boost::format as is mentioned in this SO answer.
try this one
#include<iostream>
#include<conio.h>
using namespace std;
int main(){
cout << 6+2 <<"\n" << 6-2;
}

c++ segmentation fault (core dumped) with modulus

I am working on a program that allows the user to practice division. My code is below:
//div1
#include <iostream>
#include <stdlib.h>
#include <time.h>
#include <algorithm>
using namespace std;
#define CLS "\033[2J\033[1;1H"
#define NEWLINE "\n"
int main() {
srand(time(NULL));
int a, div1, div2;
div1=rand()%11;
div2=rand()%11;
while (div2>div1) {
swap(div1,div2);
continue;
}
if (div1%div2!=0) {
return main();
} else {
cout << CLS;
cout << NEWLINE;
do {
cout << div1 << " / " << div2 << " = ?" << endl;
cin >> a;
cout << CLS;
cout << NEWLINE;
cout << "\t\tWrong!!" << endl;
cout << NEWLINE;
} while (a!=div1/div2);
cout << CLS;
cout << NEWLINE;
cout << "\t\tCorrect!!" << endl;
cout << NEWLINE;
cout << "Hit enter to continue." << endl;
cin.ignore();
cin.get();
return main();
}
return 0;
}
Basically, what it is supposed to do is first choose two random numbers. Then, it is supposed to check to see if the second number (div2) is greater than the first (div1), and if they are, it will switch them. Then, it will use the modulus (div1%div2) to make sure that the two numbers can be divided by each other without a remainder. If they cannot be divided without a remainder, it will restart the program (return main();). However, whenever I run it, I get the segmentation fault: core dumped, either when I start it or after running it a few times. Any ideas on how to fix this?
Thanks!!
Here's an example of what I've said in the comments. Obviously, you can refactor this so that it works more gracefully (as of now it'll give you floating point exceptions sometimes), but it gives you an idea on how to do this without calling main again.
NOTE: You do not need to make a constant for NEWLINE. There is already a built-in constant in std. In fact, you're already using that constant (endl). So you can just do cout << endl instead of cout << NEWLINE.
//div1
#include <iostream>
#include <stdlib.h>
#include <time.h>
#include <algorithm>
using namespace std;
#define CLS "\033[2J\033[1;1H"
#define NEWLINE "\n"
int main() {
while(true) {
srand(time(NULL));
int a, div1, div2;
div1=rand()%11;
div2=rand()%11;
while (div2>div1) {
swap(div1,div2);
continue;
}
if (div1%div2!=0) {
} else {
cout << CLS;
cout << NEWLINE;
do {
cout << div1 << " / " << div2 << " = ?" << endl;
cin >> a;
cout << CLS;
cout << NEWLINE;
cout << "\t\tWrong!!" << endl;
cout << NEWLINE;
} while (a!=div1/div2);
cout << CLS;
cout << NEWLINE;
cout << "\t\tCorrect!!" << endl;
cout << NEWLINE;
cout << "Hit enter to continue." << endl;
cin.ignore();
cin.get();
}
}
return 0;
}
This code can get into "divide by 0" error. This is why you would be getting error.
The line "if (div1%div2!=0) {" seems erroneous.
In this line if div2 == 0, then your code will crash.