years ago i was a programmer in a C derivated language that was called Pawn, now i want to try to start in C , write some things to unrust myself from this beatiful thing that is programming
No, i have never coded with C before and with any other language that was not Pawn.
This is the first time i use stackoverflow, sorry if i'm asking a stupid question.
So, this is my code:
I tried to do what the code says like its done in Pawn, almost, and well, i have errors.
#include <stdio.h>
#include <iostream> // I don't know if this is needed
#include <string> // The same for this
using namespace std; // I don't know what this means
int main()
{
int number;
printf("Write the number of croisssants you eat a day\n");
scanf("%d", &number);
printf("The number of croissants that you eat a day is %d", number);
char finalMessage[64]; // This is the way it was made in Pawn, you declare a string and put a max for text
switch(number)
{
// So here i try to use a switch to do this, like i was used to, yes i know how to use if but i'm trying to see if it's this metod is the same thing as it is in Pawn.
case 0: finalMessage = "Oh, so you don't like croissants";
case 1: finalMessage = "Thats okay";
case 2: finalMessage = "Well, if one, why not two?";
default: finalMessage = "Mmmm, i think they are too much.";
}
printf("%s", finalMessage);
return 0;
}
The errors are all this:
error: incompatible types in assignment of 'const char [11]' to 'char [64]' 20 | case 1: finalMessage = "Thats okay";
You can imagine the other ones being the same.
I tried reading a lot of "How to declare a string in C but because 2 things...
- I don't understand advanced programming terms (i.e. initialization, constant, cout, etc), i'm working in that
- English is not my first language (It's Spanish), yes, maybe i should use the Spanish forum but i'm trying to get used to the English one because i'm supposed to work with this one in the future
... i can't resolve this.
CodePudding user response:
Since you had quite a few questions packed into your code, I've made some changes and commented on them:
//#include <cstdio> // this is the correct C header, I won't use it though
#include <iostream> // I don't know if this is needed
// it's needed if you are using the C i/o streams, like std::cin and std::cout
#include <string> // The same for this
// it's needed to be able to use std::string, which you are not, but you should
// using namespace std; // I don't know what this means
// it means that you can skip writing std:: infront of all
// classes/functions/constants that are placed in the std namespace
// Don't use it for now. It's often causing problems until you know when to use it.
int main() {
int number;
//puts("Write the number of croisssants you eat a day");
std::cout << "Write the number of croisssants you eat a day\n";
/* The C way:
if (scanf("%d", &number) != 1) {
fprintf(stderr, "invalid number, bye bye\n");
return 1;
}
*/
if(!(std::cin >> number)) { // the C way:
std::cerr << "invalid number, bye bye\n";
return 1;
}
// printf("The number of croissants that you eat a day is %d", number);
// Using std::cout instead:
std::cout << "The number of croissants that you eat a day is " << number << '\n';
// char finalMessage[64];
std::string finalMessage; // use a std::string instead
switch (number) {
case 0:
finalMessage = "Oh, so you don't like croissants";
break; // needed, or else it'll continue to the next case
case 1:
finalMessage = "Thats okay";
break; // needed, or else it'll continue to the next case
case 2:
finalMessage = "Well, if one, why not two?";
break; // needed, or else it'll continue to the next case
default:
finalMessage = "Mmmm, i think they are too much.";
}
//printf("%s\n", finalMessage.c_str()); // or
//puts(finalMessage.c_str()); // or the c way:
std::cout << finalMessage << '\n';
}
CodePudding user response:
Arrays do not have the copy assignment operator.
So such an assignment like this
finalMessage = "Oh, so you don't like croissants";
is invalid.
In these statements
case 0: finalMessage = "Oh, so you don't like croissants";
case 1: finalMessage = "Thats okay";
case 2: finalMessage = "Well, if one, why not two?";
default: finalMessage = "Mmmm, i think they are too much.";
you are using string literals that have static storage duration. To make the code correct just declare the variable finalMessage
as having the type const char *
:
const char *finalMessage;
In this case in assignments like this
finalMessage = "Oh, so you don't like croissants";
the string literal is implicitly converted to a pointer to its first element and this pointer will be assigned to the pointer finalMessage
.
There is no need to use objects of the type std::string
in your simple program.
Also you need to use the statement break to pass the control after each labeled statement outside the switch statement
switch(number)
{
// So here i try to use a switch to do this, like i was used to, yes i know how to use if but i'm trying to see if it's this metod is the same thing as it is in Pawn.
case 0: finalMessage = "Oh, so you don't like croissants";
break;
case 1: finalMessage = "Thats okay";
break;
case 2: finalMessage = "Well, if one, why not two?";
break;
default: finalMessage = "Mmmm, i think they are too much.";
break;
}
As for the comment in this line
using namespace std; // I don't know what this means
then in your program it is redundant because neither name from the namespace std
is used in your program.
Standard C names as for example cout
or string
are declared in the namespace std
. Without the using directive you need to use qualified names as for example
std::string s( "Hello" );
std::cout << s << '\n';
Including the using directive you may write
using namespace std;
//...
string s( "Hello" );
cout << s << '\n';
Also pay attention to that names of headers from C you should prefix with the letter 'c' that is instead for example
#include <stdio.h>
you should write
#include <cstdio>
CodePudding user response:
Well since you are trying to get into C I would recommend ditching the pure char arrays and the C style IO (printf
and scanf
). Instead replace them with the more modern std::string
and IO (std::cout
and std::cin
).
Rewriting this in modern C would look like this
#include <iostream>
#include <string>
using namespace std;
int main()
{
int number;
cout << "Write the number of croisssants you eat a day" << endl;
cin >> number;
cout << "The number of croissants that you eat a day is " << number << endl;
string finalMessage;
switch(number)
{
case 0:
finalMessage = "Oh, so you don't like croissants";
break;
case 1:
finalMessage = "Thats okay";
break;
case 2:
finalMessage = "Well, if one, why not two?";
break;
default:
finalMessage = "Mmmm, i think they are too much.";
}
cout << finalMessage << endl;
return 0;
}
I am not familiar with pawn but in C in switch statements you have to use break
before the next case if you don't want fallthrough.
Also using namespace std;
just specifies that you are going to use the standard namespace by default. Allows you to type cout << "Something";
instead of std::cout << "Something"
. However it is considered bad practice
CodePudding user response:
It seems like you're using mostly C concepts rather than C .
In C , we would use string
instead of an array of chars. Additionally, we would use cin
(C in) and cout
(C out) for input and output. Try the following:
#include <iostream> // This is needed for cin/cout
#include <string> // This is needed to use string
using namespace std; // Basically this means you can write cin instead of std::cin, string instead of std::string, etc.
int main()
{
int number;
cout << "Write the number of croisssants you eat a day\n"; //cout instead of printf
cin >> number; //cin instead of scanf. Note that you don't need to specify types
cout << "The number of croissants that you eat a day is " << number;
string finalMessage; // In C , we use strings instead of char arrays
switch(number)
{
// Switches should work the same way
case 0: finalMessage = "Oh, so you don't like croissants"; break;
case 1: finalMessage = "Thats okay"; break;
case 2: finalMessage = "Well, if one, why not two?"; break;
default: finalMessage = "Mmmm, i think they are too much.";
}
cout << finalMessage;
return 0;
}