Why cant output Hello world

Why cant output Hello world
int main()
{
int x, c, v;
if(cin >> c){
cout << "Hello world" << endl;
}
(it doesnt says any bug)
Hey and welcome to the forum. Please read the "Before posting" article.

Since your questions are very small and related to each other, just use the same thread, you don't have to create a new one. And please use code tags for all your code so it is easier to read - http://www.cplusplus.com/articles/jEywvCM9/

Please take your time to learn the basics of c++.

The code works fine for me. It might be closing too fast as @oren below me states. You can fix that in many ways, for learning purposes just use system("pause"); at the end of your programs.
Last edited on
closed account (1vD3vCM9)
it may print it , but the program will instantly close.
and you also forgot to type this: using namespace std;
which i recommend NOT to use

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

using namespace std;

int main(){
int x,c,v;
if(cin >> c){
cout << "Hello World\n";
_getch();
}
}
Last edited on
@TarikNeaj @oren drobitsky i have typed all of that but i have to use if statement because i am using some loops
also i want to ask can i write this line of code
!cin >> c
Last edited on
Can you tell us in words what you are trying to say in code?
@TarikNeaj
1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
using namespace std;

int main()
{
    int x, c, b, m;

    if(cin >> c){
        for(x=1; x<10 && !cin >> b; x++);/*if presed b cancel loop*/
            cin >> c;
            cout << "Code " m++ << endl;
}}
Last edited on
@leardfalziu
I think TarikNeaj wanted words, not the code. He wants to know the purpose of
 
!cin >> c

What are you trying to accomplish with that line? And what actually happens when you run the program.
It would be easier if you explained exactly what you intended this program to do.


BTW, "!cin >> b" does NOT mean that it will cancel the loop if you press 'b'.
'b' in this case is the identifier of an integer type variable. It does not represent Letter 'b' on your keyboard.
You declared b to be an int type variable, so b identifies the allocated memory that is big enough to store an integer.
 
int x, c, b, m;

So x, c, b, and m are ALL integer type variables. Basically each of these can store one integer number.
Last edited on
@mpark4656
I corrected from int c to char c and when it runs it says
c /*input*/
c /*input if i input here d than...*/
c /*input*/
ccccccccc(...here it outputs ddddddddd)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
using namespace std;
int main()
{
    int x,  b, m;
    char c, m, b;
    if(cin >> c){
        for(x=1; x<10 && !cin >> b; x++);/*if presed b cancel loop*/
            cin >> m;
    if(cin >> b){
        for(x=9; x>1; x--)
       cout << "Your variable is  " m++ << endl;
    }}
}}


I want for example if i press c, i want to run the loop 9 times that means i will input 9 variables (if i press b to stop loop and to output variables form the last one to the first one i inputed
"Your variable is 'what i inputed' 1 "
"Your variable is 'what i inputed' 2 "
"Your variable is 'what i inputed' 3 "...
Last edited on
Something like this?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
Define char type variable named character;
Define bool type variable named isDone and initialize it to false;

std::cin >> character;    // Store the first user input (char) in character

// If user input is lower-case 'c', then run the loop 9 times
if ( character == 'c' ) {
    // However, if user inputs 'b', then halt the loop by assigning "true" to isDone variable
    for ( int iter = 1 ; iter <= 9 && !isDone ; iter ++ ){
        std::cout << "Variable is " << character << ' ' << iter << std::endl;

        std::cin >> character;     // Store the next user input in character variable

        // If character is a 'b', set isDone to true, thus exiting the for-loop
        if ( character == 'b' )
            isDone = true;
    }
}
    
return 0;



That was one way to do it also, you could do it this way
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Define char type variable named character;

std::cin >> character;    // Store the first user input (char) in character

// If user input is lower-case 'c', then run the loop 9 times
if ( character == 'c' ) {
    // However, if user inputs 'b', then halt the loop by assigning "true" to isDone variable
    for ( int iter = 1 ; iter <= 9 && character != 'b' ; iter ++ ){
        std::cout << "Variable is " << character << ' ' << iter << std::endl;

        std::cin >> character;     // Store the next user input in character variable
    }
}
    
return 0;


Basically, remove the Boolean variable and just let the for-loop evaluate
 
iter < 9 && character != 'b'

which translates to "Keep running the loop 9 times and as long as character does not equal 'b'

Bottom Line:

To check to see if user input is a certain character, you don't use
 
cin >> b

This only stores whatever user input is to b variable

You have to use relational operator like this
 
b == 'b'   // b is equal to 'b' 

 
b != 'b'    // b does not equal to 'b' 
Last edited on
I am computer science student so i am training myself on this project to make a program as the cashiers use.
1.As you said @mpark4656 it runs the loop just once and it doesnt interupt when i input b.
2.In this example i want also the program to output me the values that i stored from last->first but just when i press b.
3.On line 9 i wrote cin >> m; but it has to be same as the first input cin >> c because i want to store data but i know that it doesnt have to be like i wrote but just training.So how to store those values/data.
4.Do i have to use any other libraries to manipulate with data, for example in each day/weak/month/year to tell me *how many products have been sold, ** how many products with the same barcode have been sold.
Last edited on
I'd like to recap.

1. You want the program to start storing "values" when you enter 'c'.
2. After you enter 'c', you can start storing integers.
3. Once you enter the last integer, you signal the end by entering 'b'.

Here is a sample run:
User Input
c 4 6 8 98 23 45 b


Output
Stored Values are
1. 4
2. 6
3. 8
4. 98
5. 23
6. 45


Am I correct?
Last edited on
Topic archived. No new replies allowed.