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

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;
}

Related

C++ Visual studio reporting "not all paths return a value"

I'm creating a rock paper scissors game for an assignment and one of my functions is returning a warning saying "not all control paths return a value". I'm assuming the issue is with the switch statement but I'm not sure as everything in the statement is returning a value.
Here's the code if anyone can help:
int GameChoice()
{
bool loop = true;
while (loop == true)
{
system("cls");
cout << "Choose one of the following:" << endl;
cout << "[1] Rock" << endl;
cout << "[2] Paper" << endl;
cout << "[3] Scissors" << endl;
cout << "[4] Lizard" << endl;
cout << "[5] Spock" << endl;
cout << "[6] End Game" << endl;
cout << "Enter Selection: ";
int UserChoice;
cin >> UserChoice;
cout << endl;
switch (UserChoice)
{
case 1:
case 2:
case 3:
case 4:
case 5:
case 6:
loop = false;
return UserChoice;
break;
default:
cout << "Incorrect choice" << endl;
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
system("pause");
return 0;
}
}
}
I think you can ignore the warning.
In fact, before the first iteration of the while-loop is complete you will reach either return UserChoice; or return 0;.
I guess the compiler expects a return statement after the loop.

c++ writing a programm about reading from a text file and printing the whole sentence that include specific words

i am slightly rusty on programming and i was asked to write a c++ program that would read and print from a text file whole lines that include one, both or none of the words given by the user.
this is what i have until now
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
string string1,string2;
ifstream in_stream;
char choice;
void main()
{
//oppening the file needed
in_stream.open("file.txt");
//giving the strings to search
cout << "Give two strings. " << endl;
cin >> string1;
cin >> string2;
// choosing which mode to follow
cout << "Choose which mode you prefer: " << endl;
cout << "A: Both words in each line." << endl;
cout << "B: One of the words in each line." << endl;
cout << "C: None of the words in each line." << endl;
cin >> choice;
switch (choice)
{
case 'A': cout << choice << endl;
break;
case 'B': cout << choice << endl;
break;
case 'C': cout << choice << endl;
break;
default: cout << "Incorrect choice" << endl;
break;
}
system ("pause");
}
Try this implementation:
#include <iostream>
#include <string>
#include <fstream>
#include <vector>
using namespace std;
string string1,string2;
ifstream in_stream;
char choice;
void printLine(const vector<string>& _line)
{
for (const auto& word : _line)
cout << word << " ";
cout << endl;
}
void main()
{
//oppening the file needed
in_stream.open("file.txt");
vector<vector<string> > lines;
lines.push_back(vector<string>());
while (in_stream.good())
{
string1.clear();
in_stream >> string1;
if (!string1.empty())
lines.back().push_back(string1);
if (in_stream.good())
{
if (in_stream.peek() == '\n')
lines.push_back(vector<string>());
}
}
//giving the strings to search
cout << "Give two strings. " << endl;
cin >> string1;
cin >> string2;
// choosing which mode to follow
cout << "Choose which mode you prefer: " << endl;
cout << "A: Both words in each line." << endl;
cout << "B: One of the words in each line." << endl;
cout << "C: None of the words in each line." << endl;
cin >> choice;
switch (choice)
{
case 'A': cout << choice << endl;
for (const auto& line : lines)
{
if (find(line.begin(), line.end(), string1) != line.end() && find(line.begin(), line.end(), string2) != line.end())
printLine(line);
}
break;
case 'B': cout << choice << endl;
for (const auto& line : lines)
{
if (find(line.begin(), line.end(), string1) != line.end() || find(line.begin(), line.end(), string2) != line.end())
printLine(line);
}
break;
case 'C': cout << choice << endl;
for (const auto& line : lines)
{
if (find(line.begin(), line.end(), string1) == line.end() && find(line.begin(), line.end(), string2) == line.end())
printLine(line);
}
break;
default: cout << "Incorrect choice" << endl;
break;
}
system("pause");
}

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

Why is my programme stuck in the if loop?

I am new to C++ and this is like my first program I made and I used Visual C++ 2010 Express.
It is a weight conversion thing. There is an if loop, an else if loop and an else.
Here is the code:
#include <iostream>
using namespace std;
int main() {
float ay,bee;
char char1;
cout << "Welcome to the Ounce To Gram Converter" << endl << "Would you like to convert [O]unces To Grams or [G]rams To Ounces?" << endl;
start:
cin >> char1;
if (char1 = "G" ||"g"){
cout << "How many grams would you like to convert?" << endl;
cin >> bee;
cout << bee << " grams is equal to: " << bee*0.035274 << " ounces." << endl; goto start;
}
else if (char1 = "o"||"O"){
cout << "How many ounces would you like to convert" << endl;
cin >> ay;
cout << ay << " ounces is equal to: " << ay/0.035274 << " grams." << endl; goto start;
}
else{
cout << "Error 365457 The character you entered is to retarded to comprehend" << endl;
goto start;
}
cin.ignore();
cin.get();
return 0;
}
If I enter a "g", it executes this:
if (char1 = "G" ||"g"){
cout << "How many grams would you like to convert?" << endl;
cin >> bee;
cout << bee << " grams is equal to: " << bee*0.035274 << " ounces." << endl; goto start;
}
like it should
However, if I enter an "o", it executes this:
if (char1 = "G" ||"g"){
cout << "How many grams would you like to convert?" << endl;
cin >> bee;
cout << bee << " grams is equal to: " << bee*0.035274 << " ounces." << endl; goto start;
}
Instead of this:
else if (char1 = "o"||"O"){
cout << "How many ounces would you like to convert" << endl;
cin >> ay;
cout << ay << " ounces is equal to: " << ay/0.035274 << " grams." << endl; goto start;
}
Even if I put something random, like "h"
This Happens:
if (char1 = "G" ||"g"){
cout << "How many grams would you like to convert?" << endl;
cin >> bee;
cout << bee << " grams is equal to: " << bee*0.035274 << " ounces." << endl; goto start;
}
Instead of this:
else{
cout << "Error 365457 The character you entered is to retarded to comprehend" << endl;
goto start;
}
Please tell me what I did wrong.
char1 = "o"||"O" will always evaluate to true, because "O" is not null.
You want to use char1 == 'o' || char == 'O' and similar all over your if statements.
Note that = is assignment and == is an equality check. Use == when testing for equality and = when assigning. C and C++ allows you to use = in a check which returns the value of the assignment. This value is not 0, which evaluates to true and thus your if statement executes.

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