Plz help edit this c++ program?

this program is to store my name and print it. name has to have a limit of 10 characters. while loop has to end when i enter 10 characters or i press enter. but it doesn't work so . i only have to use getche() for storing the string.


MAIN PROBLEM 1: WHILE STORING THE CHARACTERS DURING'WHILE' LOOP. IT ONLY STORES CHARACTERS DURING ODD VALUE OF VARIABLE 'i'.
2:IF I REMOVE THE OPERATOR && FROM WHILE LOOP THEN IT FUNCTIONS PROPERLY

#include<stdio.h>
#include<conio.h>
#include<iostream>
using std::cout;
using std::endl;
void main(void)
{
int i=0,j=0;
char a[11];
while (getche()!='\r' && i<10)
{

a[i]=getche();
i++;
}
cout<<endl;
while(j!=i)
{
cout<<a[j];
j++;

}
getche();
}
1
2
3
4
5
6
7
8
9
10
11
12
13
#include<iostream>

int main()
{
    char ch ;
    char a[11] = { '\0' } ;
    unsigned i=0;

    while ( std::cin >> ch && ch != '\n' && i < 10 )
        a[i++] = ch ;

    std::cout << '\n' << a << '\n' ;
}


How many times do I get input per iteration of the loop? One time.

How many times do you get input per iteration of the loop? Two times. How many of those values do you store? One.
Last edited on
That's not actually the solution, Cire. He's wanting the program to stop taking input after he hits the max length (10 chars). Getche() is not standard though, zubair and it will not work on all platforms. Here's a modified version of cire's code (non standard) that produces the results you want:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include<conio.h>
#include<iostream>


int main()
{
    char ch ;
    char a[11] = { '\0' } ;
    unsigned i=0;

    do{
       ch = getche();
       a[i++] = ch;
    }while( ch!='\r' && i < 10);
    
    std::cout << '\n' << a << '\n' ;
}


Again, getche() is non standard (whereas getchar() is standard) across platforms so generally speaking this isn't ideal practice, but it does do what you ask. Like Cire said though, two loops for this is not necessary and that second loop is likely where your problem is. I'm assuming this is just to get a single name (like first name or last name) and that's why you have the carriage return as a delimiter. Otherwise if you are entering two names, it'll break after you enter the space key.

There is not a standard way of echoing input and stopping the user after a certain number of chars typed without pressing enter that I am aware of. If someone is aware of a way to do this with a standard library, by all means feel free to add to this discussion.
Last edited on
@cire http://www.cplusplus.com/forum/beginner/91858/#msg493662
How many times do you get input per iteration of the loop? Two times. How many of those values do you store? One.
How many times do you get input per iteration of the loop? Two times. How many of those values do you store? One.


yes actually it's the problem but how am i missing that one tym?
i m not getting it?

plzz will u elaborate it a little further


and btw will u plzz elaborate yr program too
i m new to programmming so u know..
Last edited on
Topic archived. No new replies allowed.