What is wrong with my c++ code? It's a short code, don't worry.?

Home » Programming & Design » What is wrong with my c++ code? It's a short code, don't worry.?
Programming & Design No Comments

Hello guys. I am trying to create a code that makes it so when the user types in a letter, and then a number, it needs to put THAT letter THAT many times in the console on a new line. I'm trying to make each alphabet letter to be typed in the console.

This is my code so far:

#include<iostream>
using

Hello guys. I am trying to create a code that makes it so when the user types in a letter, and then a number, it needs to put THAT letter THAT many times in the console on a new line. I'm trying to make each alphabet letter to be typed in the console.

This is my code so far:

#include<iostream>
using namespace std;

int main()
{
int i, j, n;
cout << "Add the number of rows :" << endl;
cin >> n;
for (i = 1; j <= 1; j++)
{
for (j = 1; j <= i; j++)
{
cout << (char)(j + 64) << "";
}
cout << endl;

}
return 0;

}

I know I must be missing something, could someone help me realize which code I'm missing so far?
So far its giving me only one error; Run-Time Check Failure #3 – "The variable 'j' is being used without being initialized." How do I overcome this? I'm new to c++, sorry guys.

Other answer:

Chris:
Check your for loops, they are using the wrong variables. You need something like

for (i = 1; i <= n; i++)

This will set i to 1, then 2, then 3, etc until n (inclusive) is reached.
Right now your code is comparing j to 1, but you didn't set j to a value first. That's why you're getting the error.

You'll also want to input a char: http://ideone.com/4TFvmp

cja:
You can avoid that error about 'j' by initializing it to zero where you declare it, i.e. : int j = 0; But your logic doesn't make sense. The outer loop checks and increments 'j', as does the inner loop. Walk through your code using pencil and paper, keeping track of the loop indexes, and see what your code is doing. You should find that no matter what number is entered, the output will be: A.

In your description of the problem you say that the user is to enter a letter and a number. You're not prompting for, or reading, a letter. Your description, together with your code, is very confusing. It is not at all clear what you're trying to do. So, my example below is probably not exactly what you need, but maybe it'll help.

#include <iostream>
#include <cctype>

using namespace std;

int main() {
. . int i, n;
. . char ch;

. . cout << "Count? : ";
. . cin >> n;
. . do {
. . . . cout << "Letter? : ";
. . . . cin >> ch;
. . } while (!isalpha(ch));

. . for (i = 0; i < n; i++) {
. . . . cout << ch;
. . }
. . cout << endl;
. . return 0;
}

#if 0

Sample run:

Count? : 5
Letter? : o
ooooo

#endif

Andrew:
Okay so from what you say you want to do this code does not work that way it takes the number inputted by the user and prints characters ranging from A to the end of ASCII code. I take it this is not the desired function based on your description of your goal though but here are the corrections I've made to your code:

#include<iostream>

using namespace std;

int main()
{
int n = 0;
cout << "Add the number of rows :" << endl;
cin >> n;
cout << endl;
for (int j = 1; j <= n; j++)
{
cout << (char)(j + 64);
}
cout << endl;
return 0;
}

As for your desired function you simply need to input a character and then a number and then use cout on the char variable within a for loop executing x number of times based on the users inputted integer

Emanuel:
you can avoid that error about 'j' by initializing it to zero where you declare it, i…e… : int j = 0; but your logic doesn't make sense… the outer loop checks and increments 'j', as does the inner loop… walk through your code using pencil and paper, keeping track of the loop indexes, and see what your code is doing… you should find that no matter what number is entered, the output shall be: a…

in your description of the problem you say that the user is to enter a letter and a number… you're not prompting for, or reading, a letter… your description, together with your code, is very confusing… it is not at all clear what you're trying to do… so, my example below is probably not exactly what you need, but maybe it'll help…

#include &lt;iostream&gt;
#include &lt;cctype&gt;

using namespace std;

int main() {
… … int i, n;
… … char ch;

… … cout &lt;&lt; "count? : ";
… … cin &gt;&gt; n;
… … do {
… … … … cout &lt;&lt; "letter? : ";
… … … … cin &gt;&gt; ch;
… … } while (!isalpha(ch));

… … for (i = 0; i &lt; n; i++) {
… … … … cout &lt;&lt; ch;
… … }
… … cout &lt;&lt; endl;
… … return 0;
}

#if 0

sample run:

count? : 5
letter? : o
ooooo

#endif

Jessica:
okay so from what you say you want to do this code does not work that way it takes the number inputted by the user and prints characters ranging from a to the end of ascii code… i take it this is not the desired function based on your description of your goal though but here are the corrections i've made to your code:

#include&lt;iostream&gt;

using namespace std;

int main()
{
int n = 0;
cout &lt;&lt; "add the number of rows :" &lt;&lt; endl;
cin &gt;&gt; n;
cout &lt;&lt; endl;
for (int j = 1; j &lt;= n; j++)
{
cout &lt;&lt; (char)(j + 64);
}
cout &lt;&lt; endl;
return 0;
}

as for your desired function you simply need to input a character and then a number and then use cout on the char variable within a for loop executing x number of times based on the users inputted integer

Andy T:
Is it a typo I see "j" in the outer for loop?
Diesel Fingers:
wasd
Chiara:
wasd
Darius:
is it a typo i see "j" in the outer for loop?
Jessica:
is it a typo i see "j" in the outer for loop?

LEAVE A COMMENT