This code, under msvc 2022:
const char* chars = "XBECEDX";
for each (char c in chars)
{
if (c == 'X') std::cout << 'A';
else
std::cout << c;
}
Ends with error E0125, E0065, E0029
Maybe output is: ABECEDA
Where is the Failure? Is it bad code or bad compiler or compiler setting or any Failure.
I solve this as vector with lambda function. Here is example:
string s = "XBECEDX";
for_each(s.begin(), s.end(), [](char ch)
{
if (c == 'X') std::cout << 'A';
else std::cout << c;
});
Why not this:
#include <iostream>
#include <string>
using namespace std;
int main(int argc, char* argv[])
{
string chars = "XBECEDX";
// char chars[] = "XBECEDX"; // will also work as an alternate to string
for (char c : chars)
{
if (c == 'X') std::cout << 'A';
else
std::cout << c;
}
return 0;
}
Related
Task:Write a code to the new string of Dna According to its pattern. Just so you know In DNA strings, symbols "A" and "T" are complements of each other, as "C" and "G".
Fore example:DNA_strand ("ATTGC") //returns "TAACG" or DNA_strand ("GTAT") //returns "CATA"
My Code=>
#include <string>
#include <vector>
#include <iostream>
std::string DNAStrand(const std::string& dna)
{
std::string Sym;
std::string c;
std::stringstream s;
s << Sym;
s >> c;
for(int i = 0; i < dna.size() - 1; i++) {
switch (dna[i]) {
case ('A'):
Sym[i] = 'T';
break;
case ('T'):
Sym[i] = 'A';
break;
case ('C'):
Sym[i] = 'G';
break;
case ('G'):
Sym[i] = 'C';
break;
default:
std::cout << "invalid";
} return c.str();
}
int main() {
std::cout << DNAStrand("ATTGC") << "\n"; //retun "TAACG"
std::cout << DNAStrand("GTAT") << "\n"; //retun "CATA"
}
}
You have created a vector<string>, but in the if statements, you are setting the elements of the vector to chars. You want to build a string, not a vector<string>.
You should replace subsequent if statements with else if, or use a switch statement. Otherwise if statements subsequent to a satisfied if statement are executed needlessly.
Replace this vector with an ostringstream. Naming the stream as s, you would append a char named c with s << c. At the end of iterating over dna, return s.str().
I was trying to make a program write 8 words to a line after a user enter their sentence.Its only printing words that have been typed in and i don't have a clue how to make it type 8 words to a line.
#include <iostream>
#include <vector>
#include <string>
#include <cctype>
using namespace std;
vector<string> sentence;
string sente = "";
void print(string, string);
template<typename T>
void print(vector<T>& v, string)
{
cout << "Enter your sentence " << endl;
getline(cin, sente);
sentence.push_back(sente);
for (auto const elem: sentence)
{
cout << elem;
}
}
int main()
{
print(sentence,sente);
}
Using global variables is generally not a good practice.
Also you don't need a extra vector for your use case.
Take a look at the following code, where you can smartly make use of istringstream for your use case:
#include <iostream>
#include <string>
#include <sstream>
void print()
{
std::string sente;
std::cout << "Enter your sentence " << std::endl;
getline(std::cin, sente);
// Used to split string around spaces.
std::istringstream ss(sente);
int wordCountPerLine = 0;
int requiredWordsPerLine = 8;
// Traverse through all words
do {
// Read a word
std::string word;
ss >> word;
// Print the read word
std::cout << word << " ";
wordCountPerLine++;
if(wordCountPerLine % requiredWordsPerLine == 0){
std::cout<<std::endl;
wordCountPerLine = 0;
}
// While there is more to read
} while (ss);
}
int main()
{
print();
}
Feel free to ask any doubts.
now I'm Having problem in repeating the loop after it finished doing the first and i want to try it again without exiting the program? I've been using while loop to do it but still no joy. so i decided to do the if statement. But the Array only accept 4 strings then it exit. Any one who can help? TIA.
#include "stdafx.h"
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
using namespace std;
template <typename T>
void GetContents(T& Input);
template <typename T>
void DisplayContents(const T& Input);
int main()
{
int PASS = 0;
// To Display the unsorted and sorted Book Titles
std::vector<std::string> books;
GetContents(books);
std::cout << "\nMy original library (number of books: " << books.size() << "):\n\n";
DisplayContents(books);
std::sort(books.begin(), books.end());
std::cout << "\nMy sorted library (number of books: " << books.size() << "):\n\n";
DisplayContents(books);
std::cout << "Press 1 to try again, else to quit: ";
std::cin >> PASS;
std::cout << "\n";
if (PASS == 1)
{
GetContents(books);
}
else
{
return 0;
}
// to input All book titles
template <typename T>
void GetContents(T& Input)
{
const int MAX = 5;
string bookName;
std::cout << "Enter a Book Titles:\n> ";
for (int i = 0; i < MAX; i++)
{
std::getline(std::cin, bookName);
Input.push_back(bookName);
std::cout <<">";
}
}
//Display All input book titles
template <typename T>
void DisplayContents(const T& Input)
{
for (auto iElement : Input)
{
std::cout << iElement << '\n';
}
std::cout << '\n';
system("pause");
}
I am working on my homework and am stuck at this place. It would he helpful if someone could help me with this. I am using Visual C++.
This is a header file with a class.
#ifndef CALLER_H
#define CALLER_H
#include <string>
#include <time.h>
#include "nodeData.h"
using namespace std;
class Caller {
private:
int phone_number;
void setPhoneNumber(){
int min = 300000;
int max = 800000;
phone_number = min + rand()%(max-min+1);
}
string callers_name;
string message;
};
#endif
I am trying to store string of name and message into the above header file but I am getting error. I have searched on google and this was one possible solution: "cin >> Caller.callers_name;" but still getting error. It is same for "cin >> Caller.message;".
int _tmain(int argc, _TCHAR* argv[])
{
QueueList L;
Caller * caller = NULL;
int choice;
const int columnSize = 3;
string columnNames[columnSize];
cout <<"\n\t\tANZ Call Center Simulation\n";
cout <<"\n\tPress 1 to make a call by a caller.\n";
cout <<"\tPress 2 to receive a call by a consultant.\n";
cout <<"\tPress 3 to print all callers.\n\tChoice: ";
cin >>choice;
if (choice == 1){
cout <<"\n\tPlease leave your name and message.\n";
cout <<"\tEnter Name: ";
cin >> Caller.callers_name;
cout <<"\n\tMessage: ";
cin >> Caller.message;
}else if (choice == 2){
}
..............
system("pause");
return 0;
}
why this program giving seg fault. I tried figuring out the issue using gdb, but no luck.
#include <iostream>
#include <condition_variable>
#include <thread>
#include <chrono>
using namespace std;
condition_variable cv;
mutex cv_m;
mutex m;
int count = 0;
#define COUNT_DONE 10
#define COUNT_HALT1 3
#define COUNT_HALT2 6
void functionCount1()
{
for(;;)
{
m.lock();
count++;
cout << "Counter value functioncount1: " << count << endl;
m.unlock();
if(count >= COUNT_DONE)
return;
}
}
void functionCount2()
{
for(;;)
{
m.lock();
count++;
cout << "Counter value functionCount2: " << count << endl;
m.unlock();
if(count >= COUNT_DONE) return;
}
}
int main()
{
thread t1(functionCount1), t2(functionCount2);
t1.join();
t2.join();
return 0;
}
Your program has undefined behavior: the accesses to count outside the mutex in functionCount1 and functionCount2 are data races. With the UB corrected, it seems fine:
#include <iostream>
#include <mutex>
#include <thread>
using namespace std;
mutex m;
int count = 0;
#define COUNT_DONE 10
void functionCount(const char* name)
{
for(;;)
{
m.lock();
auto c = ++count;
m.unlock();
cout << "Counter value " << name << ": " << c << endl;
if(c >= COUNT_DONE)
return;
}
}
int main()
{
thread t1(functionCount, "functionCount1"), t2(functionCount, "functionCount2");
t1.join();
t2.join();
}
or if you want to be "clever" and confuse your code reviewers:
void functionCount(const char* name)
{
for(;;)
{
auto c = (std::lock_guard<std::mutex>(m), count++);
cout << "Counter value " << name << ": " << c << endl;
if(c >= count_done)
break;
}
}