I created a program to pass the values from the file to the string "op", using the getline function and then print the values to the screen.
`
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
string op;
while(getline(cin, op))
{
cout << op;
}
return 0;
}
`
But, cout does not print all values. The cout only prints all values if there is an "endl".
FILE:
`
1+2
2+3\5
`
INPUT WITHOUT endl:
2+3\5
INPUT WITH endl:
1+2
2+3\5
Related
Why I'm getting error in this code
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
int main()
{
string s1= "deepak";
cout << s1 << endl;
string s2= sort(s1.begin(), s1.end());
cout << s2 << endl;
return 0;
}
ERROR :
enter image description here
main.cpp:10:20: error: conversion from 'void' to non-scalar type 'std::string'
{aka 'std""__cxx11::basic_string,char>'} requested
The sort function sorts elements in a range, but it does not return the sorted string. You can sort s1 in-place:
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
int main()
{
string s1= "deepak";
cout << s1 << endl;
sort(s1.begin(), s1.end());
cout << s1 << endl;
return 0;
}
if you want to return a sorted string:
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
string sortString(string s) {
string sortedString = s;
sort(sortedString.begin(), sortedString.end());
return sortedString;
}
int main()
{
string s1= "deepak";
cout << s1 << endl;
string sortedS1 = sortString(s1);
cout << sortedS1 << endl;
return 0;
}
I tried to write a code which gets an input from the user and concatenate with another string but it doesn't work well. The code is down below,
#include<iostream>
using namespace std;
int main() {
string s1="Hi ";
string s2;
cin>>s2;
s1=s1+s2
cout<<s1;
return 0;
}
Input:
this is how it works
Expected Output:
Hi this is how it works
But it didn't work as I expected. The output was:
Hi this
Can anybody help me?
'>>' reads space-delimited strings.
Now I found getline is used to read lines.
#include<iostream>
using namespace std;
int main() {
string s1="Hi ";
string s2;
getline(cin,s2);
s1=s1+s2;
cout<<s1;
return 0;
}
Now I get the desired output.
#include <iostream>
using namespace std;
int main()
{
string s1="hi ";
string s2;
cout << "Enter string s2: ";
getline (cin,s2);
s1 = s1 + s2;
cout << "concating both "<< s1;
return 0;
}
here use this! this should help!
well i need help in solving this issue, i want to join the arabic letters in a way to look like this " السلام عليكم " not like this " ا ل س ل ا م ع ل ي ك م "
here's my code:
#include<iostream>
#include<fstream>
#include<string>
#include<Windows.h>
#include<io.h>
#include<fcntl.h>
#include<stdlib.h>
using namespace std;
int main(){SetConsoleOutputCP(1256);
SetConsoleCP(1256);
string name;
string line;
unsigned int curline=0;
cin >> name;
ifstream thefile;
thefile.open("text.txt");
while (getline(thefile, line)){
curline++;
if (line.find(name, 0) != string::npos && name.size() ==
line.size())
cout << "found" << name<<curline;
}
thefile.close();
system("pause");
return 0;
}
now in this code i'm getting the words from a file.
am taking a class of C++ and been asked to write a program that reads integers from a file.
first request is to output all of the integers in one line
and the second request is to output the average of the integers
ive tried what has been written in the book, and when I try to cout the sum or the average, it output the addition number by number not just the total
how can I fix this? i want the simplest code possible, I dont want anything that we didnt take in class yet
#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
using namespace std;
ifstream infile;
ofstream outfile;
int main()
{
int num;
ifstream infile;
ofstream outfile;
outfile.open("Answer.txt");
infile.open("DataFile2.txt");
infile >> num;
while (infile)
{
outfile << num << " ";
infile >> num;
}
infile.close();
infile.open("DataFile2.txt");
int sum = 0;
while (infile)
{
double avg;
infile >> num;
sum = sum + num;
avg = sum / 14;
cout << endl << sum << avg;
}
}
There are a lot of mistakes in your code.You should declare the avg variable outside the while loop.Also, you should calculate the average and print it after the while loop has finished looping.Also you are dividing the sum by 14 (constant) which is not good, since you don't know how many integers are in the file.
The code should look something like this:
int sum = 0;
double avg;
while (infile) {
infile >> num;
sum = sum + num;
}
avg = sum / 14;
cout << endl << sum << avg;
I am trying to write a loop that validates user input, and then repeats if the input is bad. The input must be either a binary number (as a string) or a decimal number (as an int). I have seperate functions to validate this input, but they are not causing any trouble.
The problem arises when I select 1 or 2, and then willingly enter an invalid binary or decimal number. At this point, the do-while loop repeats successfully. The program prints another request for user input to cout, But when it comes time for the user to enter input, the program thinks that there is input in the console before I even enter anything. I believe this is a problem with whitespace/control characters in the buffer, but I am not sure how to fix it. I have tried using std::cin >> std::ws to clear any straggling white space, but no luck.
#include <iostream>
#include <string>
#include <limits>
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
using std::cout;
using std::cin;
using std::endl;
using std::numeric_limits;
using std::max;
using std::streamsize;
using std::string;
//int toDecimal;
//true is is binary
bool validateBinary(const string &binaryNumber){
for(int i = 0; i < binaryNumber.length(); i++){
if((binaryNumber[i] != 1) && (binaryNumber[i] != 0)){
return false;
}
}
return true;
}
//true if is decimal
bool validateDecimal(){
return cin;
}
int main() {
int conversionType = 0; //we initialize conversionType to a default value of 0 to ensure the copiler it will always have a value
bool isBinary = false;
bool isDecimal = false;
string binaryNumberInput;
int decimalNumberInput;
do {
if(conversionType == 0){
cout << "Enter 1 to convert binary to decimal," << endl;
cout << "2 to convert decimal to binary, " << endl;
cout << "or 3 to exit the program: ";
std::cin >> std::ws; //to clear any whitespace fron cin
cin >> conversionType; //upon a second iteration, this value is read in before a user input is given
}
if(!cin || (conversionType != 1 && conversionType != 2)){
cout << "Incorrect input." << endl;
cin.clear(); //clear the fail bit
cin.ignore(numeric_limits<streamsize>::max(), '\n'); //used to ignore not-numeric input
}
cout << "You have selected option " << conversionType << "." << endl;
if(conversionType == 1){
cout << "Please enter a binary number: ";
cin >> binaryNumberInput;
isBinary = validateBinary(binaryNumberInput);
if(!isBinary){
cout << "The numbered you entered is not a binary number!" << endl;
conversionType = 0;
}
}
if(conversionType == 2){
cout << "Please enter a decimal number: ";
cin >> decimalNumberInput;
isDecimal = validateDecimal(); //true if succeeded, meaning is a number
if(!isDecimal){
cout << "The numbered you entered is not a decimal number!" << endl;
conversionType = 0;
}
}
}
while((conversionType != 1 && conversionType != 2) || (isBinary == isDecimal));
return 0;
}
Rather than debug your current program you might want to consider using the standard library to simply things
#include <iostream>
#include <string>
#include <bitset>
#include <climits>
#include <limits>
template<typename T>
void get(T& value)
{
while (!(std::cin >> value)) {
std::cout << "Invalid input\n";
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
}
int main()
{
std::cout << "Enter 1 to convert binary to decimal,\n" <<
"2 to convert decimal to binary\n";
int option;
if (std::cin >> option) {
switch (option) {
case 1: {
std::bitset<CHAR_BIT * sizeof(unsigned long long)> bits;
get(bits);
std::cout << bits.to_ullong() << '\n';
break;
}
case 2: {
unsigned long long i;
get(i);
std::cout << std::bitset<CHAR_BIT * sizeof i>(i) << '\n';
break;
}
}
}
}
If you want this to loop you should be able to add it back in again easily enough.