Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I need to make a program that will write A+B if the symbol is +, A-B if the symbol is -, but I dont know how to declare a variable that is + or -. Thanks in advance!
strangely enough I can't find a proper duplicate.
#include <iostream>
int main(int argc, char **argv)
{
char c;
std::cin >> c;
std::cout << "this is the char I got: " << c << "\n";
return 0;
}
compile your program with
$ g++ main.cpp -o main
and then run it:
$ ./main
+
this is the char I got: +
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 months ago.
Improve this question
#include <iostream>
#include <string>
int main(){
int a, b;
std::cin >> a;
std::cin.ignore(3, '/');
std::cin >> b;
std::cout << a << " " << b;
return 0;
}
How can I change my code so that the separators do not have to be Slash characters “/” but any separator that is not an integer number.
You can't use ignore for that.
Instead use a loop to peek at and fetch the next character until the character matches your condition to stop "ignoring" character.
The characters you read that you want to "ignore", just don't do anything with them.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 12 months ago.
Improve this question
INPUT STRING
ABCE
INPUT CHAR
D
OUTPUT STRING
ABCDE
It should run for all sizes , and it should be a standard procedure ie run for all the cases.
Can anyone Help me with this? Any help would be appreciated.
You should try the std::string::insert(..). Check out the documentation
#include <iostream>
#include <string>
int main() {
std::string str("ABCE");
std::cout << str << std::endl; // ABCE
str.insert(3, 1, 'D');
std::cout << str << std::endl; // ABCDE
return -1;
}
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I want to find index of '-' in string "book-Buch". What should I do? I mustn't use this function or any others. Any ideas?
int indexOf(int ch, int fromIndex)
#include <iostream>
#include <string>
int main() {
std::string word("book-Buch");
// The easy way
std::cout << word.find("-", 0) << '\n';
// The manual way
for (std::size_t i = 0; i < word.length(); ++i) {
if (word[i] == '-') {
std::cout << i << '\n';
}
}
}
If you just want to find the index that a certain character occurs in, you just need to look at each character and check if it's the one you want.
A string can be treated as an array of characters. It's unknown whether you actually want an array of string objects, or are just confused.
Other questions that would need to be answered: do you need to find all occurrences, or just the first? Are you reading the words out of a file? You don't clearly explain how a file comes into play.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
Will fstream overwriting a file multiple times cause any problems?
No, it will not. Try this, you won't get any problems.
#include <iostream>
#include <fstream>
using namespace std;
int main(){
ofstream out;
for(int cnt = 0; cnt < 100; ++cnt){
out.open("file.txt");
out << "This will be written 100 times, and erased 99 times.";
// Uncomment the lines below if you want to see each change
//out << " Run#: " << cnt;
//cin.get();
out.close();
}
return 0;
}
Disclaimer: I did not try to run this code. Apologies if there are any syntax errors.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I want to read an input by user.
Example: If user inputs 233 245 (consider the space).
i need to assign it to variable like
a=233;
b=245;
How do i do this in c++?
You can do following:
#include <iostream>
int main(int argc, char **argv)
{
int a,b;
// Get values
std::cin >> a >> b;
// Print out values
std::cout << a << ' ' << b << '\n';
}
This reads user input from standard input to the variables a and b, and then prints them to standard output