Print address of char pointer - linux

int main(){
char a[] = "Ab";
char *ptr = a;
//ptr = a;
cout << &a << a[0] << endl;
cout << ptr << endl;
char c= 'C';
char *pC = &c;
cout << &c << " :: " << pC << endl;
cout << pC << endl;
}
Output :
0x7fffcb399550A
Ab
CAb :: CAb
CAb
Unable to understand last two outputs.

&c and pC are both pointers to a single character.
operator<< requires its operand of type char* to be a pointer to a null-terminated string, that is, to an array of characters that contains a \0 somewhere.
Otherwise the behaviour is undefined.

Related

unable to enter number in output in c++

#include<iostream>
using namespace std;
int main()
{
int num1,num2,result;
cout << "enter num 1"<<endl;
cin >> num1;
cout << "enter num 2"<<endl;
cin >> num2;
result = num1 + num2;
cout << num1 <<"+" << num2 << " = "<< result;
return 0
}
this is the code I am unable to enter in output for adding the numbers
I wrote c++ code of adding two nums when output is given to enter 1st no then I am unable to write the no how can I write that output
The last sentence your code :"return 0",didn't write ";".

How to insert a new line in a text file using C++

I have to write a program in C++ that asks user to enter lines and store them in a text file.
void create_file(char name[80])
{
char line[80],op;
ofstream fout(name);
do
{
cout << "Enter the line you want to enter in the file:" << endl << endl;
gets(line);
fout << line << endl;
cout << "\nDo you want to enter another line?" << endl;
cin >> op;
}
while(tolower(op) == 'y');
cout << "File created successfully!" << endl;
fout.close();
}
The problem is that the text is not being stored in different lines.
I have to use Turbo C++ for this program.
Minimal, Reproducible Example:
#include<iostream>
#include<fstream>
#include<conio.h>
#include<stdio.h>
#include<ctype.h>
using namespace std;
void show(char name[80])
{
char line[800];
cout << "Contents of the file:" << endl << endl;
ifstream fin (name);
while(!fin.eof())
{
fin.getline(line,80);
if(fin.eof())
break;
cout << line;
}
}
void create_file(char name[80])
{
char line[80],op;
ofstream fout(name);
do
{
cout << "Enter the line you want to enter in the file:" << endl << endl;
fflush(stdin);
gets(line);
fout << line << endl;
cout << "\nDo you want to enter another line?" << endl;
fflush(stdin);
cin >> op;
}
while(tolower(op) == 'y');
cout << "File created successfully!" << endl;
fout.close();
show(name);
}
int main()
{
char name1[80];
cout <<"Enter the name of the text file:" << endl;
gets(name1);
create_file(name1);
return 0;
}

convert string to ASCII c++

I am trying to get the program to read 2 string then convert them to ASCII value and compare the 2 value.
I am not sure how to change the string to ASCII value for the comparing function to work.
#include <iostream>
int main()
{
char ch[50];
std::cout << "Enter a character: ";
std::cin.getline(ch, sizeof(ch));
std::cout << "ASCII Value of " << ch << " is " << int(ch) << std::endl;
char cha[50];
std::cout << "Enter a character: ";
std::cin >> cha;
std::cout << "ASCII Value of " << cha << " is " << int(cha) << std::endl;
if (ch[50] == cha[50]) {
std::cout << "ASCII Value of " << cha << " is " << "equal to " << ch;
return 0;
}
else if (ch > cha) {
std::cout << ch << " is greater than " << cha << std::endl;
std::cout << int(ch) - int(cha) << "is the differents between the two " ;
return 1;
}
else
{
std::cout << ch << " is less than " << cha;
}
system("PAUSE");
}
char ch[50];
std::cout << "Enter a character: ";
std::cin.getline(ch, sizeof(ch));
char ch[50] is "array of character" otherwise known as "string"
if (ch[50] == cha[50]) { ... }
Both ch and ch50 have 50 elements. The index starts at zero, that means the last valid element is ch[49]. ch[50] is buffer overflow and causes undefined behavior.
You want instead to declare a single character, char ch; and read that character. Or declare ch[50] and read the string, and do a comparison for ch[0] (that's the first element in the index)
int main()
{
char ch;
char cha;
std::cout << "Enter one character: ";
std::cin >> ch;
std::cout << "ASCII Value of " << ch << " is " << int(ch) << std::endl;
std::cout << "Enter a character: ";
std::cin >> cha;
std::cout << "ASCII Value of " << cha << " is " << int(cha) << std::endl;
if(ch == cha)
{
std::cout << "ASCII Value of " << cha << " is " << "equal to " << ch;
}
else
{
std::cout << ch << " is greater than " << cha << std::endl;
if(ch > cha)
std::cout << ch << " is greater than " << cha << std::endl;
else
std::cout << ch << " is less than " << cha;
}
return 0;
}

One error till it works...how to fix?

#include <iostream>
#include <iomanip>
using namespace std;
int main () // print to console: 3.0*5.0=15.00
{
double a;
double b;
a =(3.0);
b =(5.0);
cout << " " << fixed << setprecision (1) << a << "\n" << endl;
cout << "* " << b << "\n" << endl;
cout << "------" << endl;
cout << fixed << setprecision (2) << a*b << "\n" << endl;
return 0;
}
int calculate () // print to console: (7.1*8.3)-2.2=56.73
{
double a;
double b;
double c;
a = (7.1);
b = (8.3);
c = (2.2);
cout << " " << fixed << setprecision (1) << a << "\n" << endl;
cout << "* " << b << "\n" << endl;
cout << "- " << c << "\n" << endl;
cout << "------" << endl;
cout << setprecision(2) << (a*b)-c << "\n" << endl;
}
int calculation () // print to console: 3.2/(6.1*5.0)=0.10
{
double a;
double b;
double c;
a=(3.2);
b=(6.1);
c=(5.0);
cout << " " << fixed << setprecision (1) << a << "\n" << endl;
cout << b << "*" << c << endl; //how can I use variables instead of using quotes?
cout << "------" << endl;
cout << setprecision(2) << a/(b*c) << "\n" << endl;
system("PAUSE");
return 0;
}
What does this output error mean? How do I fix it? someone please explain this to me. Am I suppose to add: int calculate(int a, int b, int c)?
Output:
(32): error C4716: 'calculate' : must return a value
You've declared your function as one that returns an int value but there's no return statement. Try changing the declaration to void calculate() if you don't need to return a value from it.
The calculate function needs to return a value.
You need to add something like this at the end of calculate:
return (a*b)-c;
It's only because your calculate function is supposed to return an int
and there's no return in your function.
if you don't want to return anything, you can put void calculate() instead

How to make the script rerun?

I know this is very vague but im new to C++. Im making a calculator as a first time project, and what im wanting to do is, if replied to with 'y', make the script rerun from the beginning...basically.
#include <iostream>
#include <stdio.h>
#include <math.h>
using namespace std;
int main()
{
cout << "Hello and Welcome to the Test Calculator!\n";
signed char choice;
char resp;
cout << "Choose your problem:\n a)Addition\n b)Subtraction\n c)Multiplication\n d)Division\n e)Square Root\n f)Hypotenuse\n";
scanf ("%c", &choice);
switch (choice)
{
case 'a':
{
int a;
int b;
cout << "Addition\n";
cout << "Please enter a number:\n";
cin >> a;
cout << "Please enter your second number:\n";
cin >> b;
cin.ignore();
int result = a + b;
cout << "Calculating...\n";
cout << "Your total is:\n"<<" "<<result;
cin.get();
break;
}
case 'b':
{
int c;
int d;
cout << "Subtraction\n";
cout << "Please enter a number:\n";
cin >> c;
cout << "Please enter your second number:\n";
cin >> d;
cin.ignore();
int result2 = c - d;
cout << "Calculating...\n";
cout << "Your total is:\n"<<" "<<result2;
cin.get();
break;
}
case 'c':
{
int e;
int f;
cout << "Multiplication\n";
cout << "Please enter a number:\n";
cin >> e;
cout << "Please enter your second number:\n";
cin >> f;
cin.ignore();
int result3 = e * f;
cout << "Calculating...\n";
cout << "Your total is:\n"<<" "<<result3;
cin.get();
break;
}
case 'd':
{
int g;
int h;
cout << "Division\n";
cout << "Please enter a number:\n";
cin >> g;
cout << "Please enter your second number:\n";
cin >> h;
cin.ignore();
int result4 = g / h;
cout << "Calculating...\n";
cout << "Your total is:\n"<<" "<<result4;
cin.get();
break;
}
case 'e':
{
int x;
#define square ((x)*(x))
cout << "Square Root\n";
cout << "Please enter a number:\n";
cin >> x;
cin.ignore();
cout << "Calculating...\n";
cout << "Your total is:\n"<<" "<<square;
cin.get();
break;
}
case 'f':
{
int i;
int j;
cout << "Hypotenuse\n";
cout << "Enter your smaller side:\n";
cin >> i;
cout << "Please enter the longer side:\n";
cin >> j;
cin.get();
int hypotenuse = ((i*i)+(j*j));
cout << "Calculating...\n";
cout << "The hypotenuse is the square root of:\n"<<" "<<hypotenuse;
cin.ignore();
cout << "Would you like to do another problem?\n y)Yes\n n)No\n";
cin >> resp; //this is where im trying to test this at
}
default:
{
cout << " \n";
cout << "Error: Undefined response\n";
cout << "Contact the source programmer for details\n";
}
}
}
If you put the switch into a while loop that checks to see if the char == 'n' it will continue until it finds it.
while(choice != 'n')
{
switch (choice)
{
case 'a':
{
int a;
int b;
cout << "Addition\n";
cout << "Please enter a number:\n";
cin >> a;
cout << "Please enter your second number:\n";
cin >> b;
cin.ignore();
int result = a + b;
cout << "Calculating...\n";
cout << "Your total is:\n"<<" "<<result;
cin.get();
break;
}
case 'b':
{
int c;
int d;
cout << "Subtraction\n";
cout << "Please enter a number:\n";
cin >> c;
cout << "Please enter your second number:\n";
cin >> d;
cin.ignore();
int result2 = c - d;
cout << "Calculating...\n";
cout << "Your total is:\n"<<" "<<result2;
cin.get();
break;
}
case 'c':
{
int e;
int f;
cout << "Multiplication\n";
cout << "Please enter a number:\n";
cin >> e;
cout << "Please enter your second number:\n";
cin >> f;
cin.ignore();
int result3 = e * f;
cout << "Calculating...\n";
cout << "Your total is:\n"<<" "<<result3;
cin.get();
break;
}
case 'd':
{
int g;
int h;
cout << "Division\n";
cout << "Please enter a number:\n";
cin >> g;
cout << "Please enter your second number:\n";
cin >> h;
cin.ignore();
int result4 = g / h;
cout << "Calculating...\n";
cout << "Your total is:\n"<<" "<<result4;
cin.get();
break;
}
case 'e':
{
int x;
#define square ((x)*(x))
cout << "Square Root\n";
cout << "Please enter a number:\n";
cin >> x;
cin.ignore();
cout << "Calculating...\n";
cout << "Your total is:\n"<<" "<<square;
cin.get();
break;
}
case 'f':
{
int i;
int j;
cout << "Hypotenuse\n";
cout << "Enter your smaller side:\n";
cin >> i;
cout << "Please enter the longer side:\n";
cin >> j;
cin.get();
int hypotenuse = ((i*i)+(j*j));
cout << "Calculating...\n";
cout << "The hypotenuse is the square root of:\n"<<" "<<hypotenuse;
cin.ignore();
cout << "Would you like to do another problem?\n y)Yes\n n)No\n";
cin >> choice; //this is where im trying to test this at
}
}
}
You can add another option to continue calculator. something like this:
while(doContinue == true){
switch{.....}
}
that is, enclose the switching part of code in a while loop. Set doContinue as true at first then change it at the end according to the user input.

Resources