Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C++
#include<constream.h>
void main()
{   
    clrscr();
    textmode(0);
    char a[20];
    for(int i=0;i<5;i++)
    {
        cin>>a[i];
    }
    for( i=0;i<5;i++)
    {
        cout<<a[i];
    }
    getch();
}


I m trying to make a program which take 5 names from user and print these name but i m not getting the right output....there is no error when i m running the program but not getting the right output
Posted
Updated 27-Sep-15 1:57am
v3
Comments
OriginalGriff 25-Sep-15 4:55am    
How can we tell?
We don;t know what it is meant to do, so all we can do is pick holes, like "why are you allocating 20 bytes and only using 5?", "don't use Magic Numbers", and "use sensible variable names".

So tell us what you expect it to do, and what it does do.
That way, we can perhaps help you...
Use the "Improve question" widget to edit your question and provide better information.
Member 12010794 25-Sep-15 14:07pm    
i m trying to make a program which take 5 names from user and print these name but i m not getting the right output....there is no error when i m running the program but not getting the right output
Andreas Gieriet 27-Sep-15 6:26am    
Do use std::string instead of a plain character array. You are programming C++, so use the means of C++. Your code will break if the user is not cooperating (if he knows at all how to cooperate...).
Regards
Andi

Probably you have a problem with cin buffering.
However, please note, you are using an ancient compiler and C-like strings. I would suggest your to get a more up to date compiler (there are many freely available) and use std::strings instead of character arrays.
 
Share this answer
 
If you aim to program in C++ (in contrast to C), you should use C++ means. Especially, C++ has various containers that abstract from explicit memory management. A possible solution could be:
C++
#include <vector>
#include <string>
#include <iostream>

using namespace std;

int main(void)
{
    vector<string> names;
    while(true) {
        cout << "Please enter a name (a dot ends asking for names): ";
        string name;
        cin >> name;
        if (name == ".") break;
        names.push_back(name);
    }
    for(auto it = names.begin(); it != names.end(); ++it) {
        cout << "Name: " << *it << endl;
    }
    return 0;
}
Cheers
Andi
 
Share this answer
 
v4
In the second loop you need to define a variable "i" like this, it is not defined in this scope :

C++
//for( i=0;i<5;i++)
for(int i=0;i<5;i++)
{
    cout<<a[i];
}
 
Share this answer
 
v2
Comments
[no name] 25-Sep-15 5:12am    
5 for the eagle eye
Leo Chapiro 25-Sep-15 5:36am    
Thanks, dude :)
Member 12010794 25-Sep-15 14:09pm    
bro i think i have already define "int i" in the first loop so there is no need to define it again
Kenneth Haugland 27-Sep-15 8:43am    
No, I think the dude is right :laugh:
You haven't, I think that it is declared as a local variable inside the for loop, not everywhere. That is the why It use to be at least.
Kenneth Haugland 27-Sep-15 9:00am    
5'ed. Way to go dude. :-)
Obviously, your own code is like a blackbox to you. It don't do what you expect.
What follow is not directly a solution to your problem, but a key that will help you to understand by yourself what is wrong.
The debugger is your friend. It will show you what your code is really doing.
Follow the execution, check variables and you will see that there is a point where it stop doing what you expect.

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
 
Share this answer
 
//Try this... it should work in most c or c++ compilers

#include <stdio.h>
void main()
{
    char a[100]; //buffer for 5 names of 20 chars max
                     //must insure no over run not implemented
    for (int i=0;i<5;i++)
    {
        printf("Enter a name[%i]=",i); //print a prompt
        gets(&a[i*20]);  //get the name
        //printf("\n");  //might be needed to make input on a newe line
    }
    for (i=0;i<5;i++)
        printf("Name[%i]=%s\n",i,&a[i*20]); //print name
    getchar();
}



//here is another method using C++ will not compile in a C compiler
//but it is safer to use since it utilizes the C++ std::string class
//and thus the user can type any length names without the risk of overwriting
//memory

C++
#include <stdio.h>
#include <string>
#include <iostream>
using namespace std;
void main()
{
	string a[5];
	for (int i=0;i<5;i++)
	{
	    printf("Enter a name[%i]=",i); //print a prompt
	    cin >> a[i]; //get name
	}
	for (i=0;i<5;i++)
	    cout << "name[" << i << "]=" << a[i] << "\n"; //print names
    getchar();
}
 
Share this answer
 
v3
Comments
Andreas Gieriet 27-Sep-15 6:24am    
Very unsafe coding! My advise: trash it.
First: the question was about C++, not C.
Second: using stdio.h in C++ code is a code smell.
Third: reading user data *unchecked for out-of-range* is not acceptable for any decent code.
Regards
Andi
LeumasD 27-Sep-15 6:28am    
He is writing a small quick routine to learn some C/C++
It is not a commercial application.
So I gave him a quick fix for his code... not a full solution by any standards... just a working quick routine.
Andreas Gieriet 27-Sep-15 6:36am    
Still bad advise. It's tagged C++, not C.
Regards
Andi
LeumasD 27-Sep-15 6:43am    
At least it works and fixes his code which looks like C despite the tag.

It also accomplishes the task he needed to do.

Maybe you can give him a better fix...
Andreas Gieriet 27-Sep-15 8:06am    
See my solution #5.
Cheers
Andi

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900