By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,335 Members | 2,313 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,335 IT Pros & Developers. It's quick & easy.

a few simple bugs in this code I wrote

P: 4
I'm writing a pretty basic c++ program and the same error keeps coming up "char n: redefinition."

#include <iostream>
#include <string>
using namespace std;

int main()
{
char n;

cout << "Enter a character: ";
char n = static_cast<int>(n + 1);
cin >> n;


cout << n;

system("pause");

}
May 19 '17 #1

✓ answered by donbock

What do you expect the value of n to be in the computation of n+1 inside the parentheses?

Share this Question
Share on Google+
6 Replies


Expert 100+
P: 2,400
Notice the following two lines in your code:
Expand|Select|Wrap|Line Numbers
  1. char n;
  2. ...
  3. char n = static_cast<int>(n + 1);
Both of these lines declare n as a char. You should only declare it once. Remove "char" from the second line.
May 19 '17 #2

P: 4
hm, I changed from
Expand|Select|Wrap|Line Numbers
  1. char n = static_cast<int>(n + 1);
to just
Expand|Select|Wrap|Line Numbers
  1. n = static_cast<int>(n + 1);
but the error is just "uninitialized local variable 'n' used"?
May 19 '17 #3

Expert 100+
P: 2,400
What do you expect the value of n to be in the computation of n+1 inside the parentheses?
May 19 '17 #4

P: 4
Supposed to be a +1 increase in the character int(s) read.
May 19 '17 #5

P: 4
I found it, just had to trim some fat. Your tip helped though.
May 19 '17 #6

Expert 100+
P: 2,400
Should the cin line go before the static_cast line?
May 19 '17 #7

Post your reply

Sign in to post your reply or Sign up for a free account.