How i can call function again inside another?
Exaple: cpp #include<cstdlib> #include<iostream> namespace huh { int func1(char ask) { cout << "Type something sigma here: "; cin >> ask; } int main() { if (ask = "sigma" or "Sigma") { cout << "Very sigma!" << endl; /*How do i call func1 again?*/ } } {
#Call function again
79 messages · Page 1 of 1 (latest)
When your question is answered use !solved to mark the question as resolved.
Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.
What do you mean by "call again"? You don't call func1() at all
huh::func1(//argument);
The main function is also inside the namespace huh
you only run the main function
if you want the function to be called, you must call it in the main function
You should read about functions and namespace from learncpp since there are a lot of issues tbh
i mean, i have
char ask;
cout << "Hello";
cin >> ask;
if (ask = "hi") {
cout << "";
} else {
cout << "no";
/* and i need to call that cout << "Hello"; again */
}```
You've still never called func1().
#include<cstdlib>
#include<iostream>
namespace huh {
int func1(char ask) {
cout << "Type something sigma here: ";
cin >> ask;
return 0;
}
void func2() {
cout << "Type something sigma here: ";
cin >> ask;
}
int main() {
if (ask = "sigma" or "Sigma") {
cout << "Very sigma!" << endl;
/*How do i call func1 again?*/
int res = func1('\0'); // res equals 0
func2();
}
}
{
how do i call functions then because my book doesn't say that or i didn't read abt it
thats how
Which book?
its pretty old
A book that doesn’t even explain how to call functions is pretty bad
i think i forgot or haven't gone through this topic yet
@dawn arrow I'm not being mean, but your original question just really doesn't makes sense.
You asked us how to call a function again, but you've never even called it once, and now you're mentioning that you don't know what calling a function means or how to do it
explaining its not about me
So it's hard to determine what you're really asking here.
i`ll try to explain better.
forget about first example
Ok, no worries. Let's try again. What your code snippet, and what are you trying to ask?
Okay so, im very new in C++ and im trying to make some type of CMD with custom commands and im need to take user`s command again if user type wrong command
That makes sense! So what have you got so far
#include<iostream>
#include<cstdlib>
using namespace std;
int start(char ask) {
cout << "Hello! This is MyCMD." << endl;
cout << "CMD: ";
cin >> ask;
}
int main() {
system("chcp 1251>nul");
system("title MyCMD");
if (ask = "Help" or "help") {
cout << "Here the list of avaliable commands: \nVersion -- shows current version of MyCMD" << endl;
}
else {
cout << "Wrong command! Enter 'help' to view list of avaliable commands" << endl;
for
}
return 0;
}```
so
what i have to do to make it work properly?
So going step by step, there are several issues here
int start(char ask) { ... }is defining a new function, which is never used everywhere. That can just be deleted.
ask = "Help"doesn't work the way you're expecting. It's not comparingaskto"Help". You need to use==for comparisons. This=is unconditionally assigning"Help"to theaskvariable
The or help is also not going to work
if (ask == "Help" or "help")still wouldn't work as you expect. You think it's saying ifaskis equal to either one, but it's not. You would needif (ask == "Help" or ask == "help")
lol, I spoiler it
- Near the end, you have a dangling
forthat's unused. Frankly, I'm surprised it's not a syntax error that fails to compile.
- You use the
askvariable several times, but you haven't declared it anywhere
ok ty
#include<iostream>
#include<cstdlib>
using namespace std;
int main() {
system("chcp 1251>nul");
system("title MyCMD");
char ask;
cout << "Hello! This is MyCMD." << endl;
cout << "CMD: ";
cin >> ask;
if (ask == "Help" or ask == "help") {
cout << "Here the list of avaliable commands: \nVersion -- shows current version of MyCMD" << endl;
}
else {
cout << "Wrong command! Enter 'help' to view list of avaliable commands" << endl;
}
return 0;
}```
That's a start!
incompatible operand types ("char" and "const char") and ==: no conversion of "const char [5]" to "int"
Now the next issue here is that ask is a single char. It's not a string. So ask == "Help" can't possibly work, as ask can only hold 1 character (e.g. 'H' or 'h'
oohhh
Precisely. A const char [5] is an array of 5 characters, which is how that "Help" is being interpreted. You can't compare that string to your single character (which is being implicitly promoted to int, which is why that comes up in the error message)
You can define ask as a C string:
std::string ask;
oh
(you can use more primitive C types like const char[] or const char *, but that way lies madness, so I didn't show that)
thank you, now i feel more enlightened
🙂
where?
nvm
okay, we`re done with bug fixes
lets return to the main question
in other languages i can just put everything in loop
If you don’t mind, could you mention the book you’re using to learn C++. There are a lot of bad resources when it comes to programming and using any of them can hold you back from learning efficiently.
so "CMD:" with input will appear every time
nvm i just forgot something because one day my parents asked me to do something and its ruined my discipline
Author:
Alexey Nikolaevich Vasiliev
Book name:
C++ programming in examples and tasks
Why read someone else when you can read bjarne stroustrup himself 🤔🤔
I looked it up and it doesn’t even have reviews on Amazon. That’s concerning
its popular in my region
4.9
rating
Couldn’t find it from where I’m at
@dawn arrow Has your question been resolved? If so, type !solved :)
learncpp.com is the usual recommendation around here. Very comprehensive and up-to-date. It may fill in the many gaps you are not seeing.
#include<iostream>
#include<cstdlib>
using namespace std;
int main() {
system("chcp 1251>nul");
system("title MyCMD");
string ask;
while (true) {
cout << "CMD: ";
cin >> ask;
if (ask == "Help" or ask == "help") {
cout << "Here the list of avaliable commands: \nVersion -- shows current version of MyCMD" << endl;
}
else {
cout << "Wrong command! Enter 'help' to view list of avaliable commands" << endl;
}
if (ask == "Version" or "version") {
cout << "Current version is 0.1" << endl;
}
else {
cout << "Wrong command! Enter 'help' to view list of avaliable commands" << endl;
}
}
return 0;
}```
it was so easy
bruh
!solve
Thank you and let us know if you have any more questions!
This thread is now set to auto-hide after an hour of inactivity