Error converting a char[] string to uppercase [closed] - c++

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
This code is giving me an error that sptr was not declared in the while loop.
#include<iostream>
#include<ctype.h>
#include<stdlib.h>
using namespace std;
void convertToUppercase (char *s);
main()
{
{
char s [40]="Welcome to gbk gang";
cout<<"Before converting The string is! "<<s<<endl;
void *convertToUppercase(s) ;
cout<<"After converting The string is! "<<s<<endl;}
void convertToUppercase(char *sptr)
while(*sptr!='\0')
{
if(islower(*sptr))
}
return 0 ;
}

The code shown has multiple syntax mistakes.
Starting with a spurious { that causes the code from void convertToUppercase(char *sptr) onward to become part of main() rather than be in a separate function definition. void convertToUppercase(char *sptr) itself should not compile inside of main(), you should have gotten an error about that. But while(*sptr!='\0') would also fail to compile because there is no sptr variable declared in main().
You need to remove that spurious {. Then void convertToUppercase(char *sptr) would be the start of a new function definition properly.
But you still have 2 other mistakes.
void *convertToUppercase(s) ; does not call the convertToUppercase() function. It declares a void* variable named convertToUppercase that points at the 1st char of s[].
And if(islower(*sptr)) is incomplete.
The code should look more like this instead:
#include <iostream>
#include <cctype>
using namespace std;
void convertToUppercase (char *s);
int main()
{
char s[40] = "Welcome to gbk gang";
cout << "Before converting The string is! " << s << endl;
convertToUppercase(s);
cout << "After converting The string is! " << s << endl;
return 0;
}
void convertToUppercase(char *sptr)
{
while (*sptr != '\0')
{
unsigned char ch = static_cast<unsigned char>(*sptr);
if (islower(ch)) {
*sptr = static_cast<char>(toupper(ch));
}
++sptr;
}
}
Demo

Related

c++ function input error [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 5 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
I'm writing a monte carlo simulation and my 1st function is an input but it keeps kicking back an error stating that the variable "is not declared in this scope", I tried adding the variable type in the main and it still doesn't build. I then added the variable type in the function (cin>>rounds to cin>> int rounds), and the error changed but still doesn't work. Can anyone tell me what's going on and what I need to do get the function to work.
#include <iostream>
#include <cmath>
#include <cstdlib>
#include <ctime>
int getInput();
using namespace std;
int main (void){
//set up random
srand(time(NULL));
//1st function
getInput();
}
/* #description gets a valid user input
* #param output and input
*/
int getInput(){
cout<<"enter amount of rounds";
cin>> rounds; **(error is here on line 24 ("rounds not declared in
this scope")**
}
"is not declared in this scope"
This means that where you try to use your variable (i.e. rounds), it is not known. Declaring it inside main wouldn't help, since the scope of getInput != scope of main.
You have 4 possibilities:
Declare in main and send as an argument [will be in scope for main + getInput]
Declare inside getInput [will be in scope for getInput]
Declare as global (i.e. above main) [will be in scope for everyone]
Add extern and declare wherever you like [will be in scope for everyone]
Clarification: "will be in scope for..." means "from here on..."
Here are code snippets to show your options:
/* 1st option */
void foo(int x){
x = 1;
}
int main()
{
int x;
foo(x);
return 0;
}
/*************************************/
/* 2nd option */
void foo(){
int x;
x = 1;
}
int main()
{
foo();
return 0;
}
/*************************************/
/* 3rd option */
int x;
void foo(){
x = 1;
}
int main()
{
foo();
return 0;
}
/*************************************/
/* 4th option */
void foo(){
extern int x;
x = 1;
}
int main()
{
foo();
return 0;
}
int x;
In the I would change your code into something like this:
#include <iostream>
int getInput();
using namespace std;
int main (void){
...
int in = getInput();
...
}
/* #description gets a valid user input
* #param output and input
*/
int getInput(){
int rounds;
cout << "enter amount of rounds";
cin >> rounds;
return rounds; // dont forget to return :)
}
You will need to declare the variable (as int or long whatever desired) in the function as given below:
int getInput(){
int rounds;
cout<<"enter amount of rounds";
cin>> rounds; **(error is here on line 24 ("rounds not declared in
this scope")**
}

Using built-in queue class of C++ to work within a class I create [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
For some reason, I can't seem to get the built-in queue class of C++ to work within a class I create in the way the same built-in queue works in main. I want the queue I use in my class to contain the values of the variables I add to it. But when I use the built-in queue in my class, the queue seems instead to contain something else, maybe addresses of the variables.
What am I doing wrong please?
#include <queue>
#include <iostream>
#include <cstdlib>
using namespace std;
class Myclass {
private:
queue<int> q;
public:
Myclass();
void qPush(int n){ q.push(n); }
int qFront(){ q.front(); }
void qPop(){ q.pop(); }
};
Myclass::Myclass() { // Default Constructor
}
int main () {
int num1 = 0;
int num2 = 1;
queue<int> myQ;
myQ.push(num1);
myQ.push(num2);
cout << myQ.front() << endl;
myQ.pop();
cout << myQ.front() << endl;
cout << "Myclass version: " <<endl;
Myclass b;
b.qPush(num1);
b.qPush(num2);
cout << b.qFront() << endl; // I want this to print out an int. But it looks like it may be printing out an address instead?
b.qPop();
cout << b.qFront() << endl;
return 0;
}
The output I get:
0
1
Myclass version:
537168208
537168212
The problem is that MyClass::qFront() doesn't have a return statement, and because of that it returns a garbage value.
You just need to add the return statement:
int qFront(){ return q.front(); }
To make this code work better you could also add const qualifier to make the method usable with const objects:
int qFront() const { return q.front(); }
Here is an example which demonstrates why it might be necessary:
Myclass a;
a.qPush(42);
const MyClass b = a;
cout << b.qFront(); // This line results in a error if the method isn't marked as const.
The rule here is that you should always mark methods that don't modify object state as const (if you don't have a good reason to do otherwise).
You could also add a second version (overload) of this function which would return a reference to int instead of actual int:
int qFront() const { return q.front(); }
int &qFront() { return q.front(); }
Then the first one would be used for const objects and the second one would be used for mutable ones.
Because it returns a reference to int instead of just a plain int, you could modify the returned value:
Myclass a;
a.qPush(42);
cout << a.qFront();
a.qFront() = 13;
cout << a.qFront();
(Note that compiler wouldn't allow you to write int &qFront() const { return q.front(); }, because it would allow you to modify contents of const objects, which is a bad thing.)

Using c++ and trying to "nest" some code

I'm currently trying to create a basic quiz game in C++.
The following code throws an error when I try to run it, it works if I don't use answer("answer") in the main class and instead substitute it with the actual code.
I wanted to "nest" (I don't know the technical term) some of the code so that I did not have to keep writing it out every time, as you can see i was hoping to write any question followed by answer("answer").
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
int main()
{
cout << "QUIZ C++ EDITION!\n";
cout << "Question 1:\n";
cout << "What is the colour you get when you mix red and yellow?\n\n";
question("Orange");
system("PAUSE");
}
void question(string answer) {
string input;
getline(cin, input);
if (input == answer) {
cout << "\nCorrectimundo!\n";
}
else
{
cout << "\nWrongimundo.\n";
}
return;
}
I have a feeling it's a case of wrong syntax but the IDE is not showing me where the error is unfortunately, it only happens when I run the program.
It looks like you are trying to make a function. Can I help?
– Clippy (1997-2007 RIP)
Two ways to do this. One is to forward declare question ahead of its first use.
void question(string answer);
int main()
{
...
question("Orange")
...
}
void question(string answer)
{ ... }
A forward declaration is a promise to the compiler that question will be fully defined somewhere else, maybe later in this file, maybe in another file, maybe in a library. But it must be defined somewhere or the program will compile, but it will not link.
And the other is to fully define question ahead of it's first use.
void question(string answer)
{ ... }
int main()
{
...
question("Orange")
...
}
I prefer the second approach because there is no possibility of falling into a trap like this:
void question(int answer);
int main()
{
...
question(42)
...
}
void question(string answer)
{ ... }
and getting a linker error from changing the forward declaration of question and forgetting to change the definition.
You need to provide a declaration of the function question before you can use it in main. Add
void question(string answer);
before the definition of main.
In C++ you must declare a function before you can use it (note, definition is also a declaration), but there is no mention of function question before line
question("Orange");
when it is actually trying to get called. Add a declaration:
void question(string answer);
int main()
{
// the rest of code ...
You forgot to declare your function , you can either declare it before main or just write the whole function before main.
You need to add before your main:
void question(string value);

C++ code error in my practice code [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
Can you please help me out with this issue?
#include <iostream>
#include <cstring>
using namespace std;
class A
{
public:
char str[4];
A()
{
str = "C++";
cout << "Constructor A" << endl;
}
void display()
{
cout << str << " is your name" << endl;
}
};
int main()
{
A a;
a.display();
return 0;
}
It gives the following errors:
**************************Error**********
StringProg.cpp:9: error: ISO C++ forbids initialization of member "str"
StringProg.cpp:9: error: making "str" static StringProg.cpp:9: error: invalid in-class initialization of static data member of non-integral type "char [4]"
StringProg.cpp: In member function "void A::display()":
StringProg.cpp:17: error: "str" was not declared in this scope
**************************
There are quite a few issues with C arrays that prevent you from doing what you want to do.
String literals have type of const char[n] (n being their length + 1 for \0 character). To use them in C Standard Library functions, they decay to const char*, which don't carry the size of the string, and in order to find it, the strings need to be traversed (every character being looked at and compared to \0)
As a consequence, array assignment operator would need to be rather nontrivial; this isn't provided by the language, and you have to use library functions like strcpy to move the literal into your usable memory. In other words, you can't assign C arrays like other values.
Arrays function in a very primitive way; they don't have operators for comparison, it's harder to pass them to functions and store in the classes properly.
And so, because of all the above...
Prefer std::string to char[]:
class A {
std::string str;
public:
// prefer constructor init list
A() : str("C++") {
// your line would work, too
std::cout << "Constructor A" << std::endl;
}
void display() const {
std::cout << str << " is your name" << std::endl;
}
};
int main()
{
A a;
a.display();
// return 0; is unnecessary
}
Some "rules of thumb" (rules of thumbs?): if you need more than one element, start with vector<>. Never use C arrays. string is one element, not an "array of characters".
Try the following
#include<iostream>
#include<cstring>
class A
{
private:
char str[4];
public:
A() : str { "C++" }
{
std::cout << "Constructor A" << std::endl;
}
void display() const
{
std::cout << str << " is your name" << std::endl;
}
};
int main()
{
A a;
a.display();
return 0;
}
The program output is
Constructor A
C++ is your name
Take into account that arrays do not have the copy assignment operator. Thus this statement in your program
str = "C++';
even if to update the typo and write
str = "C++";
is invalid.
You could use standard C function strcpy declared in header <cstring>. For example
#include <cstring>
//...
A()
{
std::strcpy( str, "C++" );
std::cout << "Constructor A" << std::endl;
}

why we can not reference a pointer but values [closed]

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 8 years ago.
Improve this question
#include <iostream>
using namespace std;
void reference(int &ref){
cout << ref << endl;
}
void pointer(int *ref){
cout << *ref << endl;
}
int main(){
int *i = new int[1];
*i = 10;
reference(*i); // fine
reference(i); // why not compiling!!! why not referencing to my pointer??
pointer(i); // fine
}
I want to reference a pointer, as i can see i am allowed to reference value but not pointer, why??
An object of type int* cannot be automatically converted to int&.
I think you are looking for something like:
void reference(int& ref){
cout << ref << endl;
}
void reference(int*& ref){
cout << *ref << endl;
}
Then, you can use both:
int main(){
int *i = new int[1];
*i = 10;
reference(*i);
reference(i);
return 0;
}
This line
reference(i);
is trying to pass in a int * - not an ``int` variable. Hence will not compile.
See the signature of the function
First of all "crash" is a term you can only use after getting through compiler...
void reference(int &ref)
This function is taking reference to integer as its parameter while you are passing pointer to integer through
reference(i)
Change your function's signature to something like:-
void reference(int* &ref)
for this call to work. OR change call to something like:-
int i;
reference(i);
for this function to work.